On Wed, 2018-12-26 at 10:15 +0000, Kumara, Asanka wrote: > Hi, > Is there a way to detect ALL dangling pointers (at exit) in valgrind ? > > e.g. > > char* globalVar; > > void main() > { > char* z1 = new char[10]; > char* z2 = z1; > globalVar = z2; > > delete[] z1; > > } > > At exit of the program say that “globalVar” holds a pointer to a freed memory. No, this feature does not exist.
I do not think that it would be very difficult to implement: it would basically be the same algorithm as the leak search, but instead of searching for pointers pointing at allocated blocks, the search should find the pointers pointing at recently freed blocks. That being said, it means that you will only find the dangling pointers pointing in the recently freed blocks list, as dimensioned with --freelist-vol=<number> volume of freed blocks queue [20000000] So, you would only have a guarantee to detect ALL dangling pointers if the free list volume is big enough to hold all blocks that were freed during the run of the program. And of course, as valgrind implements an optimistic leak search, you would get an 'optimistic dangling pointer search' : on a 64 bits platform, any aligned 8 bytes (e.g. integer, char array, ...) might be considered by 'luck' as pointing to a recently freed block, giving a false positive dangling pointer. Philippe _______________________________________________ Valgrind-users mailing list Valgrind-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/valgrind-users