Multiple Smart Pointer References to Same Object

larrylampco

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;

}
larrylampco

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the right smart pointer to have multiple strong references and allow mutability?

From Dev

What is the right smart pointer to have multiple strong references and allow mutability?

From Dev

Destroying multiple references to the same object in Java

From Dev

Smart pointer for holding forward references to objects

From Dev

EF 6 SaveChanges with multiple references to same (changed) object

From Dev

Smart pointer to an object that can change the pointer

From Dev

ZODB multiple object references

From Dev

Multiple object references

From Dev

Is it possible for two references to the same object to be !==?

From Dev

Multiple references pointing to the same string?

From Dev

Multiple references pointing to the same string?

From Dev

How to assign the address of an existing object to a smart pointer?

From Dev

Taking ownership from a smart pointer with further access to the raw pointer via the same smart pointer

From Dev

Getting the pointer to an object pointed towards by a smart pointer - Ivalue error

From Dev

Getting the pointer to an object pointed towards by a smart pointer - Ivalue error

From Dev

Is this a smart pointer?

From Dev

c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

From Dev

c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

From Dev

Can different smart pointers refer to the same object?

From Dev

Mapping an entity with multiple references to the same type

From Dev

ServiceStack OrmLite multiple references of same type load

From Dev

Microdata itemref - multiple references to the same item

From Dev

multiple key vault references in same ARM template

From Dev

How to safely destruct class with smart pointer to incomplete object type?

From Dev

Can a single object have multiple references?

From Dev

Get a pointer pointer for a smart pointer

From Dev

How to assign a pointer to object to another pointer to object of same class?

From Dev

Using "new" multiple times on the same pointer

From Dev

Multiple New Objects Have The Same Pointer Addresses

Related Related

  1. 1

    What is the right smart pointer to have multiple strong references and allow mutability?

  2. 2

    What is the right smart pointer to have multiple strong references and allow mutability?

  3. 3

    Destroying multiple references to the same object in Java

  4. 4

    Smart pointer for holding forward references to objects

  5. 5

    EF 6 SaveChanges with multiple references to same (changed) object

  6. 6

    Smart pointer to an object that can change the pointer

  7. 7

    ZODB multiple object references

  8. 8

    Multiple object references

  9. 9

    Is it possible for two references to the same object to be !==?

  10. 10

    Multiple references pointing to the same string?

  11. 11

    Multiple references pointing to the same string?

  12. 12

    How to assign the address of an existing object to a smart pointer?

  13. 13

    Taking ownership from a smart pointer with further access to the raw pointer via the same smart pointer

  14. 14

    Getting the pointer to an object pointed towards by a smart pointer - Ivalue error

  15. 15

    Getting the pointer to an object pointed towards by a smart pointer - Ivalue error

  16. 16

    Is this a smart pointer?

  17. 17

    c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

  18. 18

    c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

  19. 19

    Can different smart pointers refer to the same object?

  20. 20

    Mapping an entity with multiple references to the same type

  21. 21

    ServiceStack OrmLite multiple references of same type load

  22. 22

    Microdata itemref - multiple references to the same item

  23. 23

    multiple key vault references in same ARM template

  24. 24

    How to safely destruct class with smart pointer to incomplete object type?

  25. 25

    Can a single object have multiple references?

  26. 26

    Get a pointer pointer for a smart pointer

  27. 27

    How to assign a pointer to object to another pointer to object of same class?

  28. 28

    Using "new" multiple times on the same pointer

  29. 29

    Multiple New Objects Have The Same Pointer Addresses

HotTag

Archive