1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bugs fixing-dangling pointer

Discussion in 'C++' started by tommy_24, Feb 27, 2008.

  1. tommy_24

    tommy_24 New Member

    Joined:
    Feb 26, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I find very difficult to fix the dangling pointer bug occurred in my new open source office application developed in C++ language. I need some information to resolve this bug?
     
  2. imported_antony

    imported_antony New Member

    Joined:
    Feb 26, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Coverity Prevent- static source code analysis tool

    Hi,
    Usually dangling pointers arises when an object is deleted or deallocated , without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults. You didn’t mention the code for resolving the bug. For resolving this bug easily you can use Coverity prevent, a Static analysis code inspection tool for resolving defects in C++,C or Java source code. Coverity has customers like Symbian, RIM (Blackberry), Juniper networks, Cisco, Texas instruments and is also used by the Department of Homeland security to scan lots of open source projects.
     
  3. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Re: Coverity Prevent- static source code analysis tool

    One useful trick for handling dangling pointers is to initialise a pointer to NULL (before it is set to point to something), e.g. instead of int *x;, use int *x=NULL; Then when it's no longer pointing at something set it back to NULL, e.g. instead of just "free(x);", use "free(x); x=NULL;". That way all pointers will be NULL or valid, and any access to a NULL pointer will result in an immediate segfault rather than the software thinking it's pointing at something valid and continuing regardless.

    Or you can use a smart pointer class, which handles all this for you.
     

Share This Page