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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top