Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to check if pointer is dangling?

I have a code where I use a pointer to access some datablock. In some rare cases, a few members of the datablock are empty, and as a result the pointer becomes dangling. In fact, I get the correct pointer but the program crashes when trying to do something with the pointer.

The usual advice would be to avoid this type of usage. But sadly, the framework I use requires that I use this type of data access methods.

Is there a way I can "check" if the pointer is invalid before doing any operation with it? Checking that the pointer is not equal to NULL did not work, obviously. I also tried this:

try
{
    CString csClassName = typeid(*pMyPointer).name();  // Check error condition
    // The line below fails due to dangling pointer (data block is not valid).
    hr = pMyPointer->MyPointerMethod(); 
}
catch(bad_typeid)
{
    return E_FAIL;
}
catch(...)
{
    return E_FAIL;
}

Is it the correct way?

like image 460
Nikhil Avatar asked Oct 05 '11 09:10

Nikhil


People also ask

How do you identify a dangling pointer?

pointer errors The primary mechanism we use for detecting dangling pointer references (i.e., a load, store, or free to a previously freed object) is to use a distinct virtual page or sequence of pages for every dynamically allocated object.

How will you detect and avoid dangling pointer in C?

The dangling pointer errors can be avoided by initializing the pointer to the NULL value. If we assign the NULL value to the pointer, then the pointer will not point to the de-allocated memory. Assigning NULL value to the pointer means that the pointer is not pointing to any memory location.

How do you debug a dangling pointer?

To enable detection of dangling pointers, the memory debugging settings must be set to one level higher than Fast. The term free-protect appears in the Enabled Checks window. This level of memory debugging is usually all you need to find dangling pointer problems.

Is a dangling a pointer?

A dangling pointer is a pointer that occurs at the time when the object is de-allocated from memory without modifying the value of the pointer. A void pointer is a pointer that can point to any data type. It points to the deleted object. A void pointer can be assigned the address of any data type.


2 Answers

There's no way to check whether or not a raw pointer is valid. Invalid pointers are not guaranteed to fail when you access them. Instead of using a raw pointer, you need to use some form of smart pointer.

like image 118
David Heffernan Avatar answered Sep 25 '22 20:09

David Heffernan


I think you are looking at the wrong direction. You probably have a bug by which you are not correctly initializing the pointers, deleting the objects too early and trying to reuse the pointer after it has been deleted or something alike. If that is the case, you should focus on determining why that is happening and fixing the bug, rather than trying to find a way of hiding the bug.

As of the approach that you are using with the typeid operator, the answer is that it is not valid. For objects of types that don't contain virtual functions, the typeid operator is resolved at compile time based on the static type of the pointer. For objects that contain at least one virtual function, it is resolved at runtime, but calling typeid(p) with an invalid pointer is undefined behavior, and in the same way that it seems to work it might crash.

The use of smart pointers that has been suggested, might depend on what the library actually does and whether you can pass around the smart pointers at all times or not. In general it is a good idea to use smart pointers for memory management, and that will in turn guarantee that the pointers will be correctly initialized (fix if the problem is initialization) and because you no longer delete manually, chances are that if the problem is with early deletion that will no longer happen. But note that while this might solve the issue, I still think that you need to understand why the pointer is invalid in your application, as that might be a symptom of a greater problem.

Now, on the original question of how to check whether the pointer is dangling or not, you cannot do it in the program, but you can run your program inside memory debuggers (valgrind in linux, Purify or a set of others in linux) and the tool will be able to help you determining whether the pointer was never initialized, or if you released the memory to the system before the incorrect use.

like image 20
David Rodríguez - dribeas Avatar answered Sep 21 '22 20:09

David Rodríguez - dribeas