resource leak in this case?

This is a discussion on resource leak in this case? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Even if I have tested with MSVC 2008 that no resource leak, I want to confirm with you ...

  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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    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, 04: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, 01:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21