October 22, 2016
Hot Topics:

Coding Tidbits and Style That Saved My Butt

  • March 23, 2005
  • By Mike McShaffry
  • Send Email »
  • More Articles »

There are a couple of safety tips with smart pointers you should consider:

  • You can't have two different objects manage each others' smart pointers.
  • When you create a smart pointer, you have to make sure it is created straight from a raw pointer new operator.

I'll show you examples of each of these abuses. If two objects have smart pointers to each other, neither one will ever be destroyed. It may take your brain a moment to get this since each one has a reference to the other:

class CJelly;
class CPeanutButter
   shared_ptr<CJelly> m_pJelly;
   CPeanutButter(CJelly *pJelly) {m_pJelly.reset(pJelly);}

class CJelly
   shared_ptr<CPeanutButter> m_pPeanutButter;

   m_pPeanutButter.reset(new CPeanutButter(this));

void PleaseLeakMyMemory()
   shared_ptr<CJelly> pJelly(new CJelly);

If you follow the code you'll find that CJelly has two references, one from the free function and the other from the CPeanutButter. The CPeanutButter class only has one reference, but it can't ever be decremented because the CJelly smart pointer will end up with a single reference count. Basically, because they point to each other, it's almost like two stubborn gentlemen are saying, "No, sir, after you" and "Please, I insist" when trying to go through a single door. Because they point to each other, they will never be destroyed.

The solution to this is usually some kind of "owned" pointer or "weak referenced" pointer, where one object is deemed the de-factor owner, and therefore won't use the multiply referenced shared_ptr mechanism.

The other gotcha is constructing two smart pointers to manage a single object:

int *z = new int;
shared_ptr<int> bad1(z);
shared_ptr<int> bad2(z);

Remember that smart pointers work with a reference count, and each of the smart pointer objects only has one reference. If either of them goes out of scope, the memory for the object will be deallocated, and the other smart pointer will point to garbage.

More to Come

This is part 1 of a 3 part series. The next installment will appear on April 4th.

About the Author

Mike McShaffry, a.k.a. "Mr. Mike," started programming games as soon as he could tap a keyboard. He signed up at the University of Houston, where he graduated five and one-half years later. Then, he entered the boot camp of the computer game industry: Origin Systems. He worked for Warren Spector and Richard Garriott, a.k.a. "Lord British," on many of their most popular games. In 1997, Mike formed his first company, Tornado Alley. He later took a steady gig at Glass Eye Entertainment, working for his friend Monty Kerr, where he produced Microsoft Casino.

About the Book

Game Coding Complete, Second Edition
By Mike McShaffry

Published: January 14, 2005, Paperback: 850 pages
Published by Paraglyph Press
ISBN: 1932111913
Retail price: $44.99
This material is from Chapter 3 of the book.

Paraglyph Press, copyright 2005, Game Coding Complete, 2nd Edition.
Reprinted with permission.

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel