1

I am trying to handle with a code with cyclic references classes. And I found I got some problem with unique_ptr or vector, when I tried to define them as class members.

This code contains 3 classes. I will show all of them in order to avoid any ambiguity.

First, I start my code by a class called as startup in main.cpp:

#include <memory>
#include <vector>
#include "startup.h"
#include "Derived_B.h"
int main() {
    std::unique_ptr<startup> go(new startup);
    return 0;
}

In startup class, the derived class is declared (note the base class in not this one):

//Header file
#include <memory>
class startup {
public:
    startup();
    ~startup();
    class Derived_B *dcb;
};
//.cpp
#include "startup.h"
#include "Derived_B.h"
startup::startup() {
    dcb = new Derived_B(this);
}
startup::~startup() {
delete dcb;
}

Before I show the derived classes, I will show the base class:

//Header file    
#include "startup.h"
class Base {
public:
    class startup *go;
    class Derived_B *&dcb;
    Base(startup *up):go(up),
                      dcb(up->dcb){}
    ~Base(){}
};

Now, the derived class is:

//Header file    
#include <memory>
#include <vector>
#include "Base.h"
class Derived_B:public Base {
public:
    Derived_B(startup *up);
    ~Derived_B(){}
    std::unique_ptr<double[]> apple; //Unique pointer here may give problem
    std::unique_ptr<double[]> apple2; //Unique pointer here may give problem
    std::vector<double> orange; //Vector here may give problem too
};
//.cpp
#include "Derived_B.h"
Derived_B::Derived_B(startup *up):Base(up) {

}

I constructed these class in this way in order to let the startup class and Derived_B class know each orther's public member directly. However after I added unique_ptr or vector in the Derived_B class, time to time I will get problem of core dumped.

I have checked it with valgrind, and the result is here:

==7999== 1 errors in context 1 of 2:
==7999== Invalid write of size 8
==7999==    at 0x400967: _Vector_impl (stl_vector.h:87)
==7999==    by 0x400967: _Vector_base (stl_vector.h:125)
==7999==    by 0x400967: vector (stl_vector.h:257)
==7999==    by 0x400967: Derived_B::Derived_B(startup*) (Derived_B.cpp:7)
==7999==    by 0x4008F0: startup::startup() (startup.cpp:11)
==7999==    by 0x40076A: main (main.cpp:9)
==7999==  Address 0x5ab6d00 is 8 bytes after a block of size 40 alloc'd
==7999==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7999==    by 0x4008E2: startup::startup() (startup.cpp:11)
==7999==    by 0x40076A: main (main.cpp:9)
==7999== 
==7999== 
==7999== 1 errors in context 2 of 2:
==7999== Invalid write of size 8
==7999==    at 0x40095F: _Vector_impl (stl_vector.h:87)
==7999==    by 0x40095F: _Vector_base (stl_vector.h:125)
==7999==    by 0x40095F: vector (stl_vector.h:257)
==7999==    by 0x40095F: Derived_B::Derived_B(startup*) (Derived_B.cpp:7)
==7999==    by 0x4008F0: startup::startup() (startup.cpp:11)
==7999==    by 0x40076A: main (main.cpp:9)
==7999==  Address 0x5ab6cf8 is 0 bytes after a block of size 40 alloc'd
==7999==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7999==    by 0x4008E2: startup::startup() (startup.cpp:11)
==7999==    by 0x40076A: main (main.cpp:9)
==7999== 
==7999== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

Please note that the problem happens quite randomly, sometimes valgrind will report unique_ptr having problem. Could someone tell me how to solve this problem please?

Edit 2: In order to deal with the problem of cyclic reference, I used the combination of shared_ptr and unique_ptr. I will show what I have changed in the startup class and Base class:

//In startup.h
std::shared_ptr<class Derived_B> dcb;//original code: class Derived_B *dcb;
//In startup.cpp
dcb.reset(new Derived_B(this));//original code: dcb = new Derived_B(this);

//In Base.h
class startup *&go;
std::weak_ptr<class Derived_B> dcb; //original code: class Derived_B *&dcb;

At the beginning, this change seems work.But after I add new vector member in the Derived_B class, the similar problem comes again. In addition, this problem happens before the destructor. I can show part of the valgrind test:

Constructor of startup.
--12397-- REDIR: 0x54613b0 (libc.so.6:__GI_mempcpy) redirected to 0x4c34fa0 (__GI_mempcpy)
Constructor of Base.
==12397== Invalid write of size 8
==12397==    at 0x400F54: _Head_base (tuple:105)
==12397==    by 0x400F54: _Tuple_impl (tuple:202)
==12397==    by 0x400F54: tuple (tuple:602)
==12397==    by 0x400F54: unique_ptr (unique_ptr.h:415)
==12397==    by 0x400F54: Derived_B::Derived_B(startup*) (Derived_B.cpp:7)
==12397==    by 0x400C9E: startup::startup() (startup.cpp:13)
==12397==    by 0x400A6A: main (main.cpp:10)
==12397==  Address 0x5ab7168 is 0 bytes after a block of size 88 alloc'd
==12397==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12397==    by 0x400C90: startup::startup() (startup.cpp:13)
==12397==    by 0x400A6A: main (main.cpp:10)
==12397== 
==12397== Invalid write of size 8
==12397==    at 0x400F5C: _Head_base (tuple:105)
==12397==    by 0x400F5C: _Tuple_impl (tuple:202)
==12397==    by 0x400F5C: tuple (tuple:602)
==12397==    by 0x400F5C: unique_ptr (unique_ptr.h:415)
==12397==    by 0x400F5C: Derived_B::Derived_B(startup*) (Derived_B.cpp:7)
==12397==    by 0x400C9E: startup::startup() (startup.cpp:13)
==12397==    by 0x400A6A: main (main.cpp:10)
==12397==  Address 0x5ab7170 is 8 bytes after a block of size 88 alloc'd
==12397==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12397==    by 0x400C90: startup::startup() (startup.cpp:13)
==12397==    by 0x400A6A: main (main.cpp:10)
==12397== 
Constructor of Derived_B.

Edit 3 (Problem may be solved): I think the problem may be related with how I made the executable code. After I deleted all the .o files, and re-make the code, there is no more problem. Here is my Makefile:

# makefile 
FLAG = -O2 -g
COMPLIER = g++ -std=c++14
#OBJ = 
OBJ = startup.o Derived_B.o #Derived_A.o 
spTest: ${OBJ} main.cpp
    ${COMPLIER} ${FLAG} -o spTest ${OBJ} main.cpp
Derived_B.o: Derived_B.h Derived_B.cpp Base.h
    ${COMPLIER} ${FLAG} -c Derived_B.cpp 
startup.o: startup.h startup.cpp 
    ${COMPLIER} ${FLAG} -c startup.cpp
.PHONY: clean
clean:
    -rm *.o spTest
JIN TLG
  • 37
  • 6

1 Answers1

0

You're creating a circular reference by having two objects refer to each other, so you shouldn't be using a unique pointer. Try using a weak pointer.

How to break shared_ptr cyclic reference using weak_ptr

Geo
  • 61
  • 5
  • Thank you. I will try shared_ptr later on. But why the vector can also have porblem? – JIN TLG May 29 '19 at 21:59
  • Hello, I have tried to use the weak pointer. At the beginning it worked, but after I added a new vector in the Derived class member, the similar problem comes again. – JIN TLG Jun 02 '19 at 17:21