Jump to content
  • Advertisement
Sign in to follow this  
MajinMusashi

C++ delete question!

This topic is 5042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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
Quote:
Original post by joanusdmentia
Also, since SDL is in C, and not C++, you wouldn't use delete anyway, you would use free().


Oh, I've almost forgot that :)
And nice joke in your sig.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!