How to check when a pointer is deleted?
Asked Answered
G

6

8

When I debugging someone else's code, how could I found when a pointer is deleted?

Gladysglagolitic answered 26/5, 2012 at 11:21 Comment(5)
Simple: you can't.Belfast
It is not a pointer which is deleted, it is the memory zone (or data object) which is pointed by that pointer. However, see also valgrind.orgResponsiveness
In the general case, you can't, although tools like Valgrind/Purify/Insure++ can help you find bugs like double deletes and leaks in some cases.Dumb
You've tagged this as both c and c++, but free() and delete (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.Alinealinna
Gonna erase the C tag, as delete is C++.Classicist
B
5

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)

Brendis answered 26/5, 2012 at 11:24 Comment(2)
Thanks for your reply. I will try to understand your comments. But my C++ programming experience is limited to "Hello World". I hope I can find a simple solution to make my work done in 2-3 days. I comments out a 'delete' command for a pointer in the main.cpp where it is defined as char * foo="" code can run and without segment fault. But I hope to find out why.Gladysglagolitic
a pointer created by a constant like char * foo="" must not use delete. use delete only when the object has been created by an newBrendis
C
4

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:

enter image description here

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.

Chalcopyrite answered 26/5, 2012 at 12:5 Comment(2)
Thanks for your reply. But I am working on linux, and the C++ code is compiled and executed by makefile. There's no constructor/destructor in the code. the pointer is defined as 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
If what you're saying now is correct, that the pointer in question points to a literal string, then the 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
C
1

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.

Certainty answered 26/5, 2012 at 12:10 Comment(0)
S
0

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.

Septilateral answered 26/5, 2012 at 11:26 Comment(1)
Many architectures support read watchpoints. Couldn't you set a read watchpoint on the pointer?Septilateral
P
0

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!

Penitence answered 26/5, 2012 at 11:28 Comment(2)
I've seen people say it's as easy as swapping out raw pointers for shared pointers, but it just isn't true. Shared pointer copies are slow! If you use them you need to make sure you make as few copies as possible by using pass by reference whenever possible.Septilateral
@ChrisHayden: Most people don't need shared pointers. Unique pointers are fine.Classicist
C
-1

You can't. Use smart pointers and never worry about it.

Classicist answered 26/5, 2012 at 11:30 Comment(3)
Only if you have a time machine so that you can tell 'someone else' to write the code with smart pointers to begin with. Most of real world programming has some work with existing code.Sandrocottus
Or... just refactor the problematic code. The time you waste hunting down the infinity of memory errors from old bad code would be better spent refactoring it so that they do not occur. Even if you succeed in eliminating all the bugs after vastly more effort, you still can't prevent people from introducing new ones. The only general solution is to refactor.Classicist
I am ASIC designer, my "hello world" experience can't help me to rewrite the > 5 years code.Gladysglagolitic

© 2022 - 2024 — McMap. All rights reserved.