CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    1,689

    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?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Strange object question

    Quote Originally Posted by ninja9578 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured