-
January 19th, 2009, 05:35 PM
#1
Strange object question
I'm debugging a program that I wrote and I fixed a bug by adding the like
Code:
if (this == 0) return 0
This is inside of a method, I thought that if this was zero, it would fail when I tried to call the function. Can someone explain this to me?
-
January 19th, 2009, 05:37 PM
#2
Re: Strange object question
If this is null, then you have undefined behaviour. PERIOD.
Your program could do anything at anytime...(don't turn your back on it...)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 19th, 2009, 05:41 PM
#3
Re: Strange object question
Okay, thank you, that's what I was thinking. I was probably able to call the function because it had been deleted, but not removed from memory yet. I'll add some more checks to make sure it never even gets to that point.
-
January 20th, 2009, 02:43 AM
#4
Re: Strange object question
Originally Posted by ninja9578
Okay, thank you, that's what I was thinking. I was probably able to call the function because it had been deleted, but not removed from memory yet. I'll add some more checks to make sure it never even gets to that point.
It's better to change the program design. You should remove the possibility of using stale pointers by design. One alternative if you pass around objects with an unclear ownership is to use smart pointers (like shared_ptr, soon to be part of the C++ standard and already available in TR1).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|