Thread: resource leak in this case?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    resource leak in this case?

    Hello everyone,


    Even if I have tested with MSVC 2008 that no resource leak, I want to confirm with you whether it is good code to maintain resource and avoid resource leak. Also whether the code is dependent on some non-Spec regulated points, e.g. MSVC 2008 specific things. :-)

    The design pattern is, embed a sub-object into another object (e.g. embed Goo object g into Foo object), but in destructor of Foo, destructor of its sub-object (e.g. Goo object g) is not called explicitly.

    I have tested destructor of Goo object g will be called in MSVC 2008, but I am not sure whether we could rely on this -- when object goes out of scope, its sub-object also goes out of scope and destructor always gets called?

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Goo {
    public:
    	Goo()
    	{
    		cout << "constructing Goo" << endl;
    	}
    	virtual ~Goo()
    	{
    		cout << "destructing Goo " << endl;
    	}
    };
    
    class Foo {
    public:
    	Goo g;
    	
    	Foo (Goo _g) : g (_g)
    	{
    		cout << "constructing Foo " << endl;
    	}
    
    	virtual ~Foo()
    	{
    		cout << "destructing Foo " << endl;
    	}
    };
    
    int func()
    {
    	Goo g;
    	Foo f (g);
    
    	return 0;
    }
    
    int main()
    {
    	func();
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    All member variables are destroyed when an object is destroyed. An object that goes out of scope is destroyed.

    This is just ordinary composition with C++'s deterministic destruction mechanism.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    g, the variable local to func, is destroyed when func ends.

    _g, the parameter to the Foo constructor, is destroyed when the constructor ends.

    f.g, the member variable of the instance of Foo named f, is destroyed when f is destroyed, which is also when func ends.

    There are actually three instances of Goo here, although the second copy might be skipped as an optimization. All are destroyed appropriately. This is basic C++ and is much more important to know than some of the other things you've asked about. That makes me wonder if you're question is about something deeper.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Daved and laserlight,


    Question answered.

    Quote Originally Posted by Daved View Post
    g, the variable local to func, is destroyed when func ends.

    _g, the parameter to the Foo constructor, is destroyed when the constructor ends.

    f.g, the member variable of the instance of Foo named f, is destroyed when f is destroyed, which is also when func ends.

    There are actually three instances of Goo here, although the second copy might be skipped as an optimization. All are destroyed appropriately. This is basic C++ and is much more important to know than some of the other things you've asked about. That makes me wonder if you're question is about something deeper.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM