Newsgroup: comp.lang.c++
Subject: shared_ptr problems
From: cpisztest@...
Date: Thu, 24 Apr 2014 14:34:39 -0700 (PDT)
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 to As and I don't know what to do there.
I was thinking of trying to guarentee that Bs and Cs are only created by As and seeing if the compiler complained or not (unknown code base). If I can guarentee 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;
}
Subject: shared_ptr problems
From: cpisztest@...
Date: Thu, 24 Apr 2014 14:34:39 -0700 (PDT)
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 to As and I don't know what to do there.
I was thinking of trying to guarentee that Bs and Cs are only created by As and seeing if the compiler complained or not (unknown code base). If I can guarentee 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;
}
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1h3uMht
View all the progranning help forums at:
http://ift.tt/1dP9txN
No comments:
Post a Comment