After much time, I believe that the problem with my code is that I have multiple smart pointer references to the same object which causes the object to be deleted by more than one smart pointer. I have condensed the code as much as I can without taking away from the idea of the program.
Currently, the error that I am getting is a seg. fault. I believe that my double references get created at this line.
newProtocol->SetCPU(CPUPtr(this));
Is my problem a double reference to the same object and if so, how can I fix it?
#include <iostream>
#include <map>
using namespace std;
#include "boost/weak_ptr.hpp"
#include <boost/shared_ptr.hpp>
class TCPProtocol;
class CPU;
class TCPConnection;
typedef boost::shared_ptr<TCPProtocol> TCPProtocolPtr;
typedef boost::shared_ptr<CPU> CPUPtr;
typedef boost::shared_ptr<TCPConnection> TCPConnectionPtr;
class Protocol
{
public:
string GetName() const;
string _name;
};
class TCPProtocol : public Protocol
{
public:
static TCPProtocolPtr Create(){return(TCPProtocolPtr(new TCPProtocol()));}
static TCPProtocolPtr Create(const TCPProtocol &Protocol
{
return(TCPProtocolPtr(new TCPProtocol(Protocol)));
}
void SetCPU(boost::shared_ptr<CPU> CPU){ _CPU = CPU; }
protected:
boost::weak_ptr<CPU> _CPU;
};
class CPU
{
public:
typedef std::map<std::string, TCPProtocolPtr> ProtocolMap;
public:
CPU(const CPU& obj);
CPU(TCPConnectionPtr connection){}
static CPUPtr Create(const TCPConnectionPtr connection)
{
return(CPUPtr(new CPU(connection)));
}
void AddProtocol(const TCPProtocolPtr ProtocolPtr);
void SetCPU(boost::shared_ptr<CPU> CPU);
ProtocolMap Protocols();
const ProtocolMap Protocols() const;
ProtocolMap _Protocols;
};
class TCPConnection
{
public:
static TCPConnectionPtr Create()
{
return(TCPConnectionPtr(new TCPConnection()));
}
void SetVersion(int version){ _version = version; }
private:
int _version;
};
int main()
{
//Create a connection to an CPU
TCPConnectionPtr CPUConnection = TCPConnection::Create();
//Define needed connection parameters
CPUConnection->SetVersion(123);
//Create an CPU
CPUPtr CPU = CPU::Create(CPUConnection);
//Create an instance of TCPProtocol
TCPProtocolPtr ProtocolPtr = TCPProtocol::Create();
//Add the protocol to the CPU
CPU->AddProtocol(ProtocolPtr);
}
CPU::CPU(const CPU& obj)
{
//Iterate through rhs map and insert its pairs into the lhs map
for(ProtocolMap::const_iterator i = obj._Protocols.begin();
i != obj._Protocols.end(); ++i)
{
TCPProtocolPtr p = TCPProtocol::Create();
*p = *(i->second);
AddProtocol(p);
}
}
void CPU::AddProtocol(const TCPProtocolPtr ProtocolPtr)
{
TCPProtocolPtr newProtocol = TCPProtocol::Create(*ProtocolPtr);
std::string name = newProtocol->_name;
newProtocol->SetCPU(CPUPtr(this));
_Protocols[name] = newProtocol;
}
After a hint from R. Martinho Fernandes in the comments, I used
boost::enable_shared_from_this<CPU>
to get rid of the double references. The sections of code that I updated are below.
I publicly inherited from boost::enable_shared_from_this<>
and created a method called share that would return a reference to the object AND keep the reference count correct.
class CPU : public boost::enable_shared_from_this<CPU>
{
...
boost::shared_ptr<CPU> Share()
{
return shared_from_this();
}
};
void CPU::AddProtocol(const TCPProtocolPtr ProtocolPtr)
{
...
newProtocol->SetCPU(CPUPtr(this));
_Protocols[name] = newProtocol;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments