Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

SagaEterna

finding when a pointer is set to NULL

This topic is 5816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy
there is no 100% guarantee that NULL == 0.

Yes there is - the C++ Standard very clearly guarantees it.

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy
especially since there is no 100% guarantee that NULL == 0.




lol..

nice..

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!