Skip to main content
GameDev.net gamedev.net
🔒 Locked

Error with delete/free()

Started by Kudaba Aug 2, 2002 at 1:16 AM 9 replies 750+ views
Original Post
Kudaba
Kudaba
Ok here''s the situation. I have a class that I use for my heightmap, although the problem is only with the data representation. So I have a private member: float* m_pHeightData; On init I pass the x and z size of the map and create it using either m_pHeightData = new float[x*z]; or m_pHeightData = (float*)malloc(sizeof(float) * x * y); This part works fine, I''ve tried both ways of doing it and it works great. The problem comes at the end of the program, in my shutdown function for the heightmap class it simply goes: if (m_pHeightData) { // either delete [] m_pHeightData; // or (dependind on which I used to make it) free(m_pHeightData); m_pHeightData = 0; } Either way I do this it crashes, I''m using Visual Studio 6.0 on WinXP Pro. The rest of the data is fine, it''s still there and working fine right up to then end. I also made sure to delete the info before program exit by manually calling the Shutdown function. I can''t see any apparent reason for this, I''m just wondering if someone else has encountered similar situations. -Kudaba (first post in over a year..... I miss you guys)
This Space for rent.
MindCode
MindCode
Your not mixing the two, are you? Hmm...

If you hadn''t, initialize the pointer to NULL. If you did a memory release on the pointer before you allocated it (which is always a good idea) this could have been a problem.
That's just my understanding; I could be wrong.
Kudaba
Kudaba
Mixing - nope, when I first noticed this I was using new/delete then switched to malloc/free with literally the EXACT same results.

Initializing - Contstructor sets it to 0. I do have it in there to release the data if it''s inited but because it is set to 0 it skips that.

There''s always something I miss.
This Space for rent.
Crispy
Crispy
quote:
Original post by Kudaba
then switched to malloc/free with literally the EXACT same results.



That''s because the two are essentially the same.

quote:
Original post by Kudaba
Either way I do this it crashes



Try commenting out the deletion code to see if the problem is elsewhere, perhaps. In reality, the os will actually take care of your deallocating problems to a great part, it''s just not wise to always rely on it.

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
VolkerG
VolkerG
I would put a breakpoint after the new and at the line with the delete. Then have a look if m_pHeightData did change (for the same object). If so, chances are good that you messed up the contents of your object anywhere in between. If not, you have to find out if you have allready deleted the same array at another location.
Crispy
Crispy
AP, that''s why I added the "essentially" part, even accentuated it. I know they''re not the same and what you say is correct. What I was referring to in this case was the fact that for an array of elements it makes little difference whether you call free() or delete[] - if one doesn''t work, the other one has no reason to.

Thanks for clarifying that up, though!

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Kudaba
Kudaba
Crispy - If I take it out it does work fine, this is just bugging me plus I like to make sure my code is as clean as possible.

Volker - I''ve steped through it a LOT, the memory looks fine all the until the end, the pointer address is always the same and I can still look at the info in diff elements with the same results.

And yes in this situation there is basically no difference with new/delete and malloc/free because I''m just using a static type (float). Which is another reason why it should work. I''m wondering if it''s maybe a problem with XP or some weird thing, but everyone I know uses XP as well so I can''t test that theory.
This Space for rent.
Kudaba
Kudaba
Problem solved. I''ll explaine what was happening. It turns out it wasn''t directly the new/delete malloc/free code. When I generated the values in the heightmap it ended up overflowing a little but decided to not tell me. That itself was fine, other instances of the code coule be created, have heightmaps generated and then deleted fine but if it was rendered using opengl it would cause the crash. So it ended up being some weird combination of opengl using my overflowed data that caused the error.

Moral of the story. OVERFLOW BAD!

Latz
This Space for rent.
Zipster
Zipster
Were you testing in Debug mode? Because they are some extra "security" features compiled into your code that save the program from crashes when arrays are overflowed a tad.
shurcool
shurcool
try making a new project, and write a simple program (should be less than a page) to test new/malloc and delete/free. then debug it. if that works, then it''s ur proggie that screws something up. debug it too and see what''s wrong. otherwise, i''m out of it ideas. :-\

---
shurcool
wwdev

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.