shared_ptr problems

C

cpisztest

I wasn't around at design time, but I get to fix the problem...story of my life!

I know about boost::weak_ptr and would use that if the problem was limited to
classes A and B, but C has a public accessor that gives away shared_ptrs toAs and I don't know what to do there.

I was thinking of trying to guarentee that Bs and Cs are only created by Asand seeing if the compiler complained or not (unknown code base). If I canguarentee that Bs and Cs are only created by and used by As, then I can safely use weak_ptrs...I think. I could do that by hiding constructors of Bs and Cs making them private and friending them to A....I think.

Any thoughts/ideas?

The mess where nothing gets destroyed:

-----
#ifndef A_H
#define A_H

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <list>

class B;
class C;

class A : public boost::enable_shared_from_this<A>
{
public:

typedef boost::shared_ptr<A> SharedPtr;

A();
~A();

void Foo();

private:

std::list<B *> m_collectionOfBs;

void CleanupBs();
};

#endif
-----
#ifndef B_H
#define B_H

#include "A.h"
#include "C.h"

//--------
class B
{
public:

B(A::SharedPtr a);
~B();

private:

C m_c;
};

#endif
-----
#ifndef C_H
#define C_H

#include "A.h"

class C
{
public:

C(A::SharedPtr a);
~C();

A::SharedPtr GetA();

private:

A::SharedPtr m_a;
};

#endif
-----

#include "A.h"
#include "B.h"

A::A()
{
}

A::~A()
{
CleanupBs();
}

void A::Foo()
{
B * newbie = new B(shared_from_this());
m_collectionOfBs.push_back(newbie);
}

void A::CleanupBs()
{
std::list<B *>::iterator it = m_collectionOfBs.begin();

while (!m_collectionOfBs.empty() &&
it != m_collectionOfBs.end())
{
delete (*it);
it = m_collectionOfBs.erase(it);
}
}
-----
#include "B.h"

B::B(A::SharedPtr a)
:
m_c(a)
{
}

B::~B()
{
}
-----
#include "C.h"

C::C(A::SharedPtr a)
:
m_a(a)
{
}

C::~C()
{
}

A::SharedPtr C::GetA()
{
return m_a;
}
-----
#include "A.h"

int main()
{
A::SharedPtr a(new A());
a->Foo();

return 0;
}
 

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,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top