Vasaka
Vasaka

Reputation: 2022

Catching delete of a stack object?

Are there tools to catches deletion of a stack object, how likely that gcc and Visual Studio debug build will break on that event immediately?

I deal with big, old projects, so that is not a question on how to write code, but how to detect and fix problems.

Upvotes: 2

Views: 145

Answers (2)

valdo
valdo

Reputation: 12943

Most of the heap implementations are not tolerate to invalid pointers (i.e. when you delete an address which was not returned to you by the heap). Almost for sure standard windows heap, and CRT's heap (implemented by MSVC) cause a debug breakpoint in such a case.

You may also replace the implementation of new/delete operators and do the checking yourself (in your case you only want to check if the address belongs to the stack memory, which is easy).

Upvotes: 2

Puppy
Puppy

Reputation: 146910

It's very unlikely that such a thing would happen if you design the ownerships in advance and use smart pointers to enforce them if necessary. I doubt that they'll do anything but throw on a generic bad deallocation.

Upvotes: 2

Related Questions