Click here to Skip to main content
14,695,615 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello I'n new on the forum, meaning It's my first question asked but I recently came across some strange behavior.

I have two classes Goo and Foo. I have a method in Foo that returns a shared pointer of type Goo. It all compiles ok...but when I debug it I can see that 2 objects are created of type Goo and the member class inside Foo never gets a value.

here's the code :

#include <memory>
#include <iostream>
#include <vector>
using namespace std;

class Goo
{
	int y;
public:
	Goo(){}
	~Goo(){}
	void set_y(int new_val) 
	{
		y=new_val;
	}

};

class Foo
{
	
	int x;
	//shared_ptr<Goo> gooptr;
	Goo Goomem;

	public:

	Foo(int val=0) :x(val) {}
	~Foo(){cout<<"desctructed";}
	
	void do_something() 
	{
		cout<<"doing nothing..."<<endl;
	}
	void set_x(int new_val) 
	{
		x=new_val;
	}
	void show_x() const {cout<< x<<endl; }
	shared_ptr<Goo> getshared()
	{
		shared_ptr<Goo> gooptr = make_shared<Goo>(Goomem);
		return gooptr;
	}

};


int main()
{
	bool expired=false;
	shared_ptr<Foo> pf (new Foo); 

	shared_ptr<Goo>gp=(*pf).getshared();
	(*gp).set_y(1);
	gp=(*pf).getshared();
	(*gp).set_y(2);
	gp=(*pf).getshared();
	(*gp).set_y(3);
	gp=(*pf).getshared();
	(*gp).set_y(4);
	gp=(*pf).getshared();

	pf.reset();

	return 0;
}


There must be something I'm missing...I mean I think that make_shared actually creates a new instance every time and since there are no more smart pointers that point to it, since i'm reassigning the goo pointer, it get's garbage collected ? I don't know...(Sorry if it's obvious for you but I just started to learn about them and C in general)
Posted
Comments
E.F. Nijboer 9-Mar-12 8:54am
   
Goomem is never initialised.
   
Gomem is not a member class, this member is an instance of the class; and the pointer are pointers to instances. In your terminology, don't mix types and objects of types, as a minimum, it makes reading difficult. Not always correct meaning could be assumed, because there could be different objects of the same type, etc.
--SA
   
Not clear. Which are two objects of Goo? You create only one object Foo, and Goomem inside...
Did you go with the debugger? In particular, put a break point on a Goo destructor (or write some log code in it).
--SA
m0rTu 12-Mar-12 9:22am
   
It works ok...I mean it does what it's supposed to...make_shared actually creates a new instance of GOO and that what ends up at gooptr...

1 solution

This method:
shared_ptr<goo> getshared()
{
	shared_ptr<goo> gooptr = make_shared<goo>(Goomem);
        return gooptr;
}</goo></goo></goo>

reates a new instance of Goo each time it is called and the old instance get's garbage colected because the are no more references to it.
this is how I actually wrote the value inside Goo
(*pf).setgoo((*gp).get_y());

pretty spaghetti if you ask me...but it worked
thanks to all
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900