Jump to content
  • Advertisement
Sign in to follow this  
CyberSlag5k

What are some things that would cause free() to crash?

This topic is 4877 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 have a pointer to struct I've created built into a class. When the user presses a button, he/she is prompted to select a file from which to load data and memory is allocated (via malloc) for the pointer. Then when the user presses another button, data is calculated and fed into the dynamic array I have allocated. At the end of the function called by that button press, I call free() on the pointer and set it to NULL. The source file is also reset, so that the function that processes the data cannot be re-called until the function that allocates the space is re-called. Anyway, everything works until the second time I attempt to free the memory and I get an error saying there is an unhandled exception. Here's a brief summary: button 1 pressed (function 1 called) file selected space allocated (malloc) button 2 pressed (function 2 called) data processed (and output) space freed (free)* pointer set to NULL Everything works great, until the line with the asterisk (*) on the second time through, which blows up. The code is somewhat massive, I use the pointer in question a ton, or I would post the code. Really I'm just looking for some suggestions as to what to check to see why the call to free would be blowing up. If there is any more information I can provide, I would be happy to do so. I'm not sure what more I can say, though. Everything works perfectly the first time through, and I do set each pointer to NULL after calling free on it, so I would think that running things a second time wouldn't do anything different. I am, of course, wrong. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
If you're using classes, why not use new/delete instead of malloc/free? Anyway, you're probably overwriting the bounds of the space you allocated. If you're stuck for ideas on debugging it, try freeing it the moment you allocate it, as a test. If that works, you know the allocation and deallocation itself is fine (as it should be). Then move the free call to half-way through the processing. If that works, you know the problem is in the latter half of the data processing, etc.

Share this post


Link to post
Share on other sites
If you pass free() an address you got by some other means than malloc(), then prepare for the absolute worst.

Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
If you pass free() an address you got by some other means than malloc(), then prepare for the absolute worst.


Yep.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
If you're using classes, why not use new/delete instead of malloc/free? Anyway, you're probably overwriting the bounds of the space you allocated. If you're stuck for ideas on debugging it, try freeing it the moment you allocate it, as a test. If that works, you know the allocation and deallocation itself is fine (as it should be). Then move the free call to half-way through the processing. If that works, you know the problem is in the latter half of the data processing, etc.


Ah, that would make sense. I could be freeing memory that doesn't belong to me. I shall scour my code and utilize the technique you suggested.

Thanks, Kylotan!

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!