finding when a pointer is set to NULL

Started by
60 comments, last by SagaEterna 21 years, 9 months ago
Hello, here''s my problem, i work with a high level abstraction engine. One member of a class is set to NULL by the engine, but i cannot trace the code to find when (because the high level of abstraction). Is there a way with visual c++ to find when the pointer is set to NULL? Thanks.
sorry for my bad english, i like to eat frogg's legs :)
Advertisement
if(prt == NULL)

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
if (!ptr)
{

}

Even neater
Use a data breakpoint. First, find a function that sets the pointer in question to a non-NULL value. Put a normal breakpoint in that function using F9. Let the program run to that breakpoing. Then find out the address that that pointer points to. Try adding that pointer to the watch window, for example. Once you know the address that is overwritten with NULL, hit Alt-F9, and go to data. Enter (int *) 0x12345678, replacing 0x12345678 with the actual address. Hit F5, and when someone sets the value of that pointer to NULL you should be broken on the according line.

---
Come to #directxdev IRC channel on AfterNET
Check out setting conditional breakpoints in MSDN.
quote:Original post by Mr Sam
if (!ptr)
{

}

Even neater



I dear to disagree. This kind of syntax is IMO okay when dealing with numerical values, such as integers, to check if the value of the int is zero. Currently we''re dealing with a totally different concept, pointers. Pointers never have a zero value, they either point to something, or not. I agree, it is legal to write if(!ptr), but I would personally discourage you from mixing up real data members and pointers at this point (no pun inteded ) - if your pointer points to nothing, it is a NULL-pointer, it does not have the value zero! Hence, in case of pointers, it would be clearer to write if(ptr == NULL), especially since there is no 100% guarantee that NULL == 0.

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
quote:Original post by Crispy
there is no 100% guarantee that NULL == 0.

Yes there is - the C++ Standard very clearly guarantees it.
quote:Original post by Crispy
especially since there is no 100% guarantee that NULL == 0.



lol..

nice..
lol, i guess you learn new things every day

tomorrow i might be told that a byte isn''t actually 8 bits..
quote:Original post by Crispy
I dear to disagree. ... Hence, in case of pointers, it would be clearer to write if(ptr == NULL), especially since there is no 100% guarantee that NULL == 0.


Even ignoring the fact that NULL is guaranteed to be 0, this way of testing the pointer value leaves you vulnerable to a particularly nasty typo bug which can cause horrible problems in your code:

if(ptr = NULL){  //  error handling code}  


This is nasty because it mucks up your pointer, but the error handling code never gets called. So your program will crash horribly for no apparent reason. In fact, it wouldn't surprise me if something like this was causing the problem the original poster is complaining about - it is an easy mistake to make, and it can be a right bastard to spot.

It is safer to reverse the expression, e.g:

if(NULL == ptr){}  


This way, if you accidentally use = instead of ==, the compiler will complain.

[edit] to answer the original question:

Use the debugger to set a watch on the variable to see where it gets set to NULL. You can also try looking at the stack dump to see which functions are being called leading up to the error, to help narrow it down.




[edited by - Sandman on July 15, 2002 7:38:33 AM]

This topic is closed to new replies.

Advertisement