resource leak in this case?

Joined
Feb 12, 2008
Messages
108
Reaction score
0
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top