[RESOLVED] Slight crashing problem with my Code.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] Slight crashing problem with my Code.

  1. #1
    Join Date
    Jun 2009
    Posts
    100

    [RESOLVED] Slight crashing problem with my Code.

    Ok I know this MAY not be the correct spot to put this but it has to do with C++ and I am ALMOST positive its not an allegro library error.

    Everything compiles perfectly but it crashes when it loads my convertme function.

    Basically this program is designed to read a text file of values created from another program that says whether a pixel is black or white and then moves to the next pixel until the image is finished.

    Well this interpritation program is supposed to take those 1 and 0 numbers and print it accordingly on a bitmap surface and then copy that temp bitmap surface onto the backbuffer and then copy the backbuffer onto the surface.

    After doing code-breaks in my program I noticed that it runs fine until It loads my convertme function and then it crashes.

    The code compiles fine so what am I doing wrong? :-/



    <CODE>

    #include <allegro.h>
    #include <fstream>
    #include <iostream>
    #include <string>

    using namespace std;

    BITMAP *pic = NULL;

    BITMAP *buffer = NULL;

    BITMAP *pic2 = NULL;

    volatile long speed_counter = 0;


    void increment_speed_counter()

    {

    speed_counter++;

    }

    END_OF_FUNCTION(increment_speed_counter);

    void shutdown()

    {

    destroy_bitmap(pic);
    destroy_bitmap(buffer);
    destroy_bitmap(pic2);

    }

    void convertme(string filename, int picWidth)

    {

    string line;

    ifstream g;

    g.open(filename.c_str());

    int X = 0;

    int Y = 0;

    while (! g.eof() )

    {

    getline (g,line);

    if (line == "0")

    {
    putpixel(pic2,X,Y, makecol( 255, 255, 255));
    }

    if (line == "1")

    {
    putpixel(pic2,X,Y, makecol( 0, 0, 0));
    }

    if (X == picWidth)

    {
    Y = Y + 1;
    X = 0;
    }

    if (X < picWidth)

    {
    X = X + 1;
    }


    }

    g.close();

    }

    int main(int argc, char *argv[])

    {

    int pic_x = 0;
    int pic_y = 0;

    allegro_init();
    install_keyboard();
    install_timer();

    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);

    install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 200,200,0,0);

    allegro_message("Attempting to Convert Text to Image!");

    convertme("Test.txt",32);

    draw_sprite(pic, pic2, pic_x, pic_y);

    buffer = create_bitmap(200,200);



    while(!key[KEY_ESC])

    {

    while(speed_counter > 0)

    {

    if(key[KEY_RIGHT])

    {
    pic_x ++;
    }

    else if(key[KEY_LEFT])
    {
    pic_x --;
    }

    else if(key[KEY_UP])

    {
    pic_y --;
    }

    else if(key[KEY_DOWN])

    {
    pic_y ++;
    }

    speed_counter --;

    }

    if(pic_x > 170)

    {
    pic_x --;
    }

    if(pic_x < 0)

    {
    pic_x ++;
    }

    if(pic_y > 165)

    {
    pic_y --;
    }

    if(pic_y < 0)

    {
    pic_y ++;
    }




    draw_sprite(buffer, pic, pic_x, pic_y);

    blit(buffer, screen, 0,0,0,0,200,200);

    clear_bitmap(buffer);

    }

    shutdown();

    return(0);

    }

    END_OF_MAIN()


    </CODE>

    I am sure its something stupid on my end. :-/

    Also I couldn't figure out how to use a code block on here.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Slight crashing problem with my Code.

    Well, the first thing I notice is this:

    Code:
    while (! g.eof() )
    {
        getline (g,line);
    For some reason, this approach seems to be common, but in practice it will often lead to off-by-1 errors. It's a lot safer to just do
    Code:
    while (getline(g, line))
    {
    Second, I notice that you're declaring pic and pic2 as global pointers, but I *don't* see where you're pointing them at anything valid before trying to use them. Am I just missing something?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Slight crashing problem with my Code.

    Quote Originally Posted by David2010 View Post
    Everything compiles perfectly but it crashes
    I see this statement many times from posters.

    It doesn't really matter if the code compiles perfectly. All that means if the code compiles is that the code is syntactically correct. Whether it is logically correct, runs correctly, and produces the results you want is a totally different story.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Sep 2008
    Posts
    113

    Re: Slight crashing problem with my Code.

    Quote Originally Posted by David2010 View Post
    Also I couldn't figure out how to use a code block on here.
    Forums typically use BBCODE style tags which are surrounded by square brackets [], not pointed ones <>.

    So <code></code> should be [ code][ /code] (without the spaces).

  5. #5
    Join Date
    Jun 2009
    Posts
    100

    Re: Slight crashing problem with my Code.

    Quote Originally Posted by Lindley View Post
    Well, the first thing I notice is this:

    Second, I notice that you're declaring pic and pic2 as global pointers, but I *don't* see where you're pointing them at anything valid before trying to use them. Am I just missing something?
    Doy. Ok I removed pic2 completely and used pic in its place. :-?

    I made them global so they could be accesed from my entire program including my convertme function.

    I thought I pointed them to something...

    I set "pic" to the correct pixel values...

  6. #6
    Join Date
    Jun 2009
    Posts
    100

    Re: Slight crashing problem with my Code.

    Quote Originally Posted by Paul McKenzie View Post
    I see this statement many times from posters.

    It doesn't really matter if the code compiles perfectly. All that means if the code compiles is that the code is syntactically correct. Whether it is logically correct, runs correctly, and produces the results you want is a totally different story.

    Regards,

    Paul McKenzie
    Is there any decent debugger for allegro or C++ so I could tell what was going on?

    I tried setting breakpoints and popup messages to find information but it crashes during the convertme function. :-/

  7. #7
    Join Date
    Jun 2009
    Posts
    100

    Re: Slight crashing problem with my Code.

    Quote Originally Posted by Raislin View Post
    Forums typically use BBCODE style tags which are surrounded by square brackets [], not pointed ones <>.

    So <code></code> should be [ code][ /code] (without the spaces).

    Ahh. Cool. Thanks!

  8. #8
    Join Date
    Jun 2009
    Posts
    100

    Re: Slight crashing problem with my Code.

    Ok after a little bit more debugging I found that this line of code in my convertme function is what is causing it to crash. Sadly though I don't know why. :-/

    Code:
    putpixel(pic,X,Y, makecol( 255, 255, 255));

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Slight crashing problem with my Code.

    As far as I can see, the only thing you ever set pic to is NULL. You never direct the pointer to an actual BITMAP object.

  10. #10
    Join Date
    Jun 2009
    Posts
    100

    Re: Slight crashing problem with my Code.

    Thanks I fixed it!

    It was crashing because, as a previous user pointed out, I didn't set it to anything. (Sorry still learning).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center