Sign in to follow this  
MajinMusashi

C++ delete question!

Recommended Posts

I'm using Visual C++ 6.0 with SDL, and I'm in doubt about the following situation: Uint8 *keys; keys = SDL_GetKeyState(NULL); delete keys; When the application ends, and I'm in debugging mode, it throws me a "User breakpoint called from code at 0x7c901230" in the moment I try to delete "keys". If I've allocated memory, I must deallocate it, right? Why is this happening? Thanks!! PS.: this is just curiosity. Not deleting "keys" will make the app ends fine...

Share this post


Link to post
Share on other sites
My guess would be that you haven't allocated any memory. This confirms it.

Any time you are given a pointer, check the documentation carefully to see who is responsible for the memory it points to. In this case, the SDL maintains ownership of the memory, so it will free it when the time comes to do so. If you're getting a breakpoint, there's a pretty good chance that memory wasn't dynamically allocated in the first place, but other times you won't be so lucky.

CM

Share this post


Link to post
Share on other sites
SDL_GetKeyState() returns a pointer to an internal array on stack, which you must not delete.
Rule of thumb: Only 'delete' pointers you created with 'new'. When in doubt (e.g. using a 3rd party library like SDL), consult the documentation.

Share this post


Link to post
Share on other sites
Quote:
Straight from the docs:
Gets a snapshot of the current keyboard state. The current state is returned as a pointer to an array. The size of this array is stored in numkeys. The array is indexed by the SDLK_* symbols (see SDLKey). A value of 1 means the key is pressed and a value of 0 means its not. The pointer returned is a pointer to an internal SDL array and should not be freed by the caller.

I'm kind today so I won't shout rtfm at you [wink].

Regards,
Pat.

Share this post


Link to post
Share on other sites
[quote]Original post by darookie
Quote:

SDL_GetKeyState() returns a pointer to an internal array on stack, which you must not delete.
Rule of thumb: Only 'delete' pointers you created with 'new'. When in doubt (e.g. using a 3rd party library like SDL), consult the documentation.
I'm kind today so I won't shout rtfm at you [wink].

Regards,
Pat.


It can look very strange, but this function is not in my SDL docs, so I thought it could be an undocumented one (it happens [wink]).
But thanks!

PS.: RTFM? What is that? ;) Hehe.

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
My guess would be that you haven't allocated any memory. This confirms it.

Any time you are given a pointer, check the documentation carefully to see who is responsible for the memory it points to. In this case, the SDL maintains ownership of the memory, so it will free it when the time comes to do so. If you're getting a breakpoint, there's a pretty good chance that memory wasn't dynamically allocated in the first place, but other times you won't be so lucky.

CM


Thanks, now I completely understand what was going there!
I love you all [wink]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this