When I debugging someone else's code, how could I found when a pointer is deleted?
1)
Use a debugger. follow one delete. In general you end up in some "free" function passing a pointer
Set a breakpoint with the condition that the past pointer has the same value as your investigated pointer
2)
One similar approach is to override the "delete" method and to check for that pointer in question.
3)
If the pointer refers to an object with an destructor. Place a breakpoint on the destructor. May be you may want to add a destructor first (if possible at all by foreign code, always possible on own code)
char * foo=""
code can run and without segment fault. But I hope to find out why. –
Gladysglagolitic char * foo=""
must not use delete. use delete only when the object has been created by an new –
Brendis Set a conditional breakpoint on the destructor of the type in question. Let the condition be that this
points to the object you're interested in. E.g., in Visual C++ Express 2010:
For the above figure I first executed to after the three new
expressions, then noted the address of the b
object, and then used as breakpoint condition that this
should be that address.
The details of how to do this with other debuggers depends on the debugger. See the debugger's manual.
char* foo=""
and deleted as delete [] foo
. Comments out the delete line, everything looks fine. So I hope to find in the code where it the pointer foo is deleted. –
Gladysglagolitic delete[]
that you deleted was the problem. Except for delete
of a nullpointer it's UB to delete[]
a pointer that hasn't been obtained by new[]
. I suspect, however, that what you're saying isn't entirely correct, and in that case, instead of debugging, use a std::string
. –
Chalcopyrite In C++ you don't have any inbuilt cross platform feature, which will find whether a pointer is deleted or not.
However, you can use the facilities provided by some debuggers, tools and the language itself. For example you can overload the new
and delete
operator globally and/or on per class bases and maintain a common set/map kind of reference. e.g.:
class X {
...
set<void*> m_CurrentAlloc;
public:
void* operator new (size_t SIZE)
{
...
m_CurrentAlloc.insert(p);
return p;
}
void operator delete (void *p)
{
m_CurrentAlloc.erase(p);
...
}
};
On periodic bases or at end of the program the contents of this set
can be printed or verified.
Remember that this is a solution for an ideal situation, where you are doing memory management using new/delete
. If you are having mix of malloc/free
too then code need other enhancements too.
How about a GDB watchpoint? You can set a watchpoint on the pointer in question and see when the program accesses to delete its referent.
If you are referring to the memory pointed to by a pointer, you can't. However, if you are having trouble with dangling pointers, just replace all of their raw pointers with boost::shared_ptr and remove all occurences of free and delete. Never use the delete or free keywords. Smart pointers rock!
You can't. Use smart pointers and never worry about it.
© 2022 - 2024 — McMap. All rights reserved.
free()
anddelete
(assuming you may be referring to one or more of these) are very different, and there would be different ways of detecting either in debugging. It might be good to elaborate in your question about what you're trying to do, and which one (or both) you mean. – Alinealinnadelete
is C++. – Classicist