Geeks With Blogs
Timo Heinäpurola

We all know how at some point a small typo can result in odd application behavior even if the source code it self compiles. One issue that pops up from time to time is missing the ‘&’ character and thus declaring a variable inadvertently as not being a reference.

void SomeMethod()
{
    // ...

    RRArray<RRInt_t> rIndices = pSubMesh->GetIndices();

    // ...
}

Just recently I ran into this kind of a situation where I had a method that contained this kind of an invalid variable definition (GetIndices returns a reference to an instance of RRArray<RRInt_t>). The bug was actually quite hard to track down, since problems popped up only after the object referenced by pSubMesh was destructed.

First of all I had not explicitly defined a copy constructor for RRArray. This meant that the default copy constructor just copied the internal array pointer to the new instance. When the scope of rIndices was left the object’s internal memory was released. When the array object owned by the sub mesh was destructed, however, that same internal memory had already been released.

This doesn’t happen that often but when it does, it’s quite hard to spot it as the problem might seem quite disjoint from it’s consequences.

Posted on Thursday, May 26, 2011 6:29 AM C/C++ | Back to top


Comments on this post: Referential danger

# re: Referential danger
Requesting Gravatar...
The Mariner 1 is probably the best example of how small mistakes in coding can have huge consequences.
Left by Rickey Fain on May 26, 2011 2:17 PM

# re: Referential danger
Requesting Gravatar...
No matter how good you are at it all wrong from time to time. Especially when you write code in a notepad is most evident.
Left by Mandy More on May 14, 2012 1:05 PM

Your comment:
 (will show your gravatar)


Copyright © raccoon_tim | Powered by: GeeksWithBlogs.net