Jump to content
  • Advertisement
Sign in to follow this  
mr_jrt

Virtual Base Classes, Virtual Destructors and pain

This topic is 4839 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

[edit] Save yourself the trouble of reading this whole post, I just missed something obvious...[/edit] I've got some very strange shenanagains going on in my Visual Studio. Basically, here's a simplification of the classes involved (it's really much simpler than it looks, it's just somewhat verbose): a) iCaption is an interface that returns a string. (Used so generated strings can be used interchangably with strings when needed.) b) iPosition is an interface that shows the object has a postion and is movable. c) iFont is an interface that represents a text object that appears in the scene, it "is-a iPositional" d) iGameObject is an interface that represents a generic object that appears in the scene, it "is-a iPositional" e) cFont is just a concrete class that implements the iFont interface, and it "has-a iCaption" that it generates its text from f) cGameObject is just a concrete class that implements the iGameObject interface, and it "has-a cFont", 'theCaption', which it uses as a kind of label Also, all interfaces are inherited virtually. Ok, thems the classes, here's the problem. Everything works fine until it's time to clean up. As I delete these objects via pointers to their interfaces, virtual destructors are required. Things proceed as you'd expect (rough pseudo-ordering):
delete Puck // (cGameObject*)
  =
  ~cPuck()
  ~cGameObject()
  {
    delete theCaption // (iFont*)
      =
      ~cFont()
      ~iFont()
      ~iPositional()
  }
  ~iPositional()
...if that makes any sense. The problem comes when 'theCaption' is deleted. It also "is-a iPositional", but for some reason (I'm guessing the compiler gets confused as both are "iPositional" the generated assembly jumps back into cGameObject's destructor and I get a stack overflow as it recurses ad infinitum. I suspect my abundance of virtual bits and bobs is the cause, but I'm fairly sure that the destructor of an aggregate class shouldn't be jumping around back into the destructor of it's containing class irregardless of how badly it's designed. I've re-built the whole project several times to no avail (just incase it was mixing stale object files), the dodgy code is always there. The only way to stop it happening that I've found is to delete 'theCaption' manually *before* I delete the cGameObject properly. A pain yes, but managable. Thoughts anyone as to whether it's a bug or this is the expected behaviour? (Visual C++ 2003, btw) - Jamie [Edited by - mr_jrt on August 17, 2005 8:19:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
Just ignore that, found the problem. Double-deleting something that would take twice as long as the above to explain. Suffice to say, I'm feeling quite stupid. [headshake]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!