Is there some trick to get valgrind to detect this sort of dangling 
pointer error?

cat >test.cpp <<EOD
#include <iostream>
int sub(void) {
      int *p;
      {
         int x = 123;
         p = &x;
      }
      std::cout << "value of p " << *p << std::endl;
      return *p;
}
int main() {
      int ret = sub();
      std::cout << "value of ret " << ret << std::endl;
      return ret;
}
EOD
g++ -Wall -g -O0 -o test test.cpp
./test
value of p 123
value of ret 123
valgrind ./test
# no problems reported

If sub() instead uses an explicit

    p = (int *) malloc(sizeof(int));
    *p = 123;
    free(p);

then valgrind sees the use of memory after free.  But in the original it 
seems that x is on the stack,
and there is never an explicit delete() when the variable goes out of 
scope, so nothing tells valgrind
that that memory is no longer valid.

(This came up on the Inkscape developer list, originally in reference to 
the warnings clang emits.)

Thanks,

David Mathog
mat...@caltech.edu
Manager, Sequence Analysis Facility, Biology Division, Caltech

------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to