Sign in to follow this  
Gazoo101

Heap Corruption ahoy

Recommended Posts

Gazoo101    100
Hey All,

My program crashes violently when I perform a delete [] command on an existing array. Originally, it only crashed when completely detached from a debugger (using Visual Studio 2010 btw). At that point I googled and found a bunch of recommendations for application verifier. I downloaded that but found that when I attached it, the program no longer crashed and then verifier reported no errors...

However, after fiddling a bit more in Vs2010, I got my program to crash in release mode with the debugger attached. I thought I was clever when printing where the array pointer I am deleting points to, as I thought it would get overwritten and thus cause a crash when being deleted.

No such luck... I'm pretty confused right now. The array is a size of 1, never changes address, or at least - not from it's created all the way up to when it's deleted, and yet delete [] still causes an access violation...

Anyone have any ideas of what might help me pinpoint the problem?

Regards,

Gazoo

Share this post


Link to post
Share on other sites
the_edd    2109
Presumably it was created with new[], rather than new, or malloc. And you aren't mixing runtimes? Do you have any linker warnings?

Have you tried using a std::vector<> instead, at least temporarily? The stronger type-checking involved with a class vs a pointer might help to catch a mistake elsewhere.

Share this post


Link to post
Share on other sites
ApochPiQ    23003
Make sure you aren't deleting it twice. Also, make sure you aren't overrunning the array bounds anywhere, because that can cause all manner of chaos.

As suggested, don't use raw arrays in the first place. They're prone to precisely this kind of nasty mistake, and basically are almost never justified in C++.

Share this post


Link to post
Share on other sites
Gazoo101    100
Heyo!

Thank you for the replies. I'm using an external library, so unfortunately I cannot rely on an std::vector. But I have done some good by moving a large portion of the memory allocation from the heap to the stack by turning things into references instead of using pointers.

My crashing still seems to wander back and forth between code segments. However, right now it seems to occur a lot when the stringstream object is automatically brought out of scope. I read a bit around on the net and found one of the most common problems is returning objects that immediately go out of scope, like

[code]
return myStringstream.str().c_str()
[/code]

BUT! All of my calls to the aforementioned double function happen when calling another function that takes a cstring. My question is, shouldn't the objects still be in scope at that point? Lookey code example here:

[code]
std::stringstream myStringstream;

afunc(myStringstream.str().c_str());
[/code]

Is that bad? Do I still need to allocate memory on the heap for the final cstring and copy it there while the function runs?

Gazoo

Share this post


Link to post
Share on other sites
rip-off    10976
[quote]
I'm using an external library, so unfortunately I cannot rely on an std::vector.
[/quote]
Are you sure? You can usually pass std::vector instances to external libraries by using &v.front(), v.size() (for the common case where the API expects a pointer to T and a length).

Share this post


Link to post
Share on other sites
Hodgman    51231
[quote name='Gazoo101' timestamp='1311500737' post='4839533']My program crashes violently when I perform a delete [] command on an existing array.[/quote]Can you post some code describing this? How is the array created in the first place? Are you using DLLs at all?[quote]But I have done some good by moving a large portion of the memory allocation from the heap to the stack by turning things into references instead of using pointers.[/quote]This sounds a bit suss, because you can use both references and pointers to refer to both stack and heap allocated objects...[quote]Is that bad? Do I still need to allocate memory on the heap for the final cstring and copy it there while the function runs?[/quote]No that should be fine, assuming that the function does not copy the input pointer into a member variable, or keep hold of it in some other way.

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