Error freeing surface?

[…]

    cout << "Leaving.." << endl;
    for(int i=(IMAGES-1); i>0; --i) {
            cout << "Freeing surface " << i << " ["
                 << image_file[i] << "]..";
            SDL_FreeSurface( image_surface[i] );
            cout << endl;

            if( image_surface[i] ) {
                    cout << "error!" << endl;
                    exit(1);
            } else {
                    cout << "ok" << endl;
            }

    }
    cout << "Calling SDL_Quit()..";
    SDL_Quit();
    cout << "done." << endl;

My surfaces are loaded and displayed correctly. But when exiting my
program parashutes.
None of my surfaces seem to pass this test.
How come?–

-Stephan /
/ tisprut productions [tm]

        http://wiktor.dk/~stephan

[…]

    cout << "Leaving.." << endl;
    for(int i=(IMAGES-1); i>0; --i) {
            cout << "Freeing surface " << i << " ["
                 << image_file[i] << "]..";
            SDL_FreeSurface( image_surface[i] );
            cout << endl;
            if( image_surface[i] ) {
                    cout << "error!" << endl;
                    exit(1);
            } else {
                    cout << "ok" << endl;
            }
    }
    cout << "Calling SDL_Quit()..";
    SDL_Quit();
    cout << "done." << endl;

My surfaces are loaded and displayed correctly. But when exiting my
program parashutes.
None of my surfaces seem to pass this test.
How come?

Freeing a pointer doesn’t set the pointer to NULL.

Common free code:

if ( ptr ) {
free(ptr);
ptr = NULL;
}

if ( ptr ) {
cout << “pointer hasn’t been freed” << endl;
}

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

Freeing a pointer doesn’t set the pointer to NULL.

In C++, deleting allocated memory pointer to by *p, sets *p
to 0.

if ( ptr ) {
free(ptr);
ptr = NULL;
}

Is NULL and 0 the same when talking pointers?

Anyway, the problem wasn’t related to the freeing-code.

I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.

When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.

What can possibly be the cause of this?On Sun, 19 Dec 1999, Sam Lantinga wrote:

-Stephan /
/ tisprut productions [tm]

        http://wiktor.dk/~stephan

Stephan Dragehjerte wrote:

Freeing a pointer doesn’t set the pointer to NULL.

In C++, deleting allocated memory pointer to by *p, sets *p
to 0.

Most definitely not.

if ( ptr ) {
free(ptr);
ptr = NULL;
}

Is NULL and 0 the same when talking pointers?

In C++, the old NULL macro is deprecated and using just plain 0 is
recommended. The null pointer is not necessarily all-bits-zero, but when
0 is used as a literal pointer constant, it means the null pointer.

Anyway, the problem wasn’t related to the freeing-code.

I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.

When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.

What can possibly be the cause of this?

Just be aware that memory problems such as deleting a pointer twice, or
using freed memory, can have very weird symptoms that vary across runs,
compilations, and architectures. What appears to work may not actually
work.> On Sun, 19 Dec 1999, Sam Lantinga wrote:


Marc Lepage
http://www.antimeta.com/
Minion open source game, RTS game programming, etc.

Marc Lepage once said:

Anyway, the problem wasn’t related to the freeing-code.

I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.

When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.

What can possibly be the cause of this?

Just be aware that memory problems such as deleting a pointer twice, or
using freed memory, can have very weird symptoms that vary across runs,
compilations, and architectures. What appears to work may not actually
work.

The crash has the same pattern. The first time the window is opened
and closed it works well. The second time the windows is opened and
then closed it hangs.

I’ve tried not calling SDL_Quit(), and then it works fine – except
the window isn’t closed and stays on the screen. But further calls to
the function updates the window and activates it (makes it accept
events as usual).

The SDL_Surface *screen usind in many of the SDL-demos as the
screen, on which all the graphics are draw… is it supposed to be
freed as well? (I’ve tried, but still no success).

I am not freeing pointers twice. Besides isn’t not the
SDL_FreeSurface that hangs.

Btw, what does the atexit(SDL_Quit) do? I implemented it because I
saw it in a demo and though it was useful, but uncommenting it doesn’t
change anything. Is it event-related somehow?–

-Stephan /
/ tisprut productions [tm]

        http://wiktor.dk/~stephan