c++ - 由于具有循环引用的类中的 unique_ptr 或 vector 而导致核心转储

标签 c++ vector unique-ptr coredump cyclic-reference

我正在尝试处理带有循环引用类的代码。当我尝试将它们定义为类成员时,我发现 unique_ptr 或 vector 存在一些问题。

此代码包含 3 个类。我将展示所有这些以避免任何歧义。

首先,我从 main.cpp 中名为 startup 的类开始我的代码:

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

在启动类中,声明了派生类(注意不是这个中的基类):
//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;
}

在展示派生类之前,我将展示基类:
//Header file    
#include "startup.h"
class Base {
public:
    class startup *go;
    class Derived_B *&dcb;
    Base(startup *up):go(up),
                      dcb(up->dcb){}
    ~Base(){}
};

现在,派生类是:
//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) {

}

我这样构造这些类是为了让启动类和 Derived_B 类直接知道彼此的公共(public)成员。但是,在我在 Derived_B 类中添加 unique_ptr 或 vector 后,有时我会遇到核心转储的问题。

我已经用valgrind检查过,结果在这里:
==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)

请注意,这个问题是随机发生的,有时 valgrind 会报告 unique_ptr 有问题。
有人可以告诉我如何解决这个问题吗?

编辑 2:
为了处理循环引用的问题,我使用了shared_ptr和unique_ptr的组合。我将展示我在启动类和基类中所做的更改:
//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;

一开始,这种改变似乎可行。但是在我在 Derived_B 类中添加新的 vector 成员后,类似的问题又来了。另外,这个问题发生在析构函数之前。我可以展示部分 valgrind 测试:
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.

编辑 3(问题可能已解决):
我认为问题可能与我如何制作可执行代码有关。删除所有.o文件后,重新制作代码,就没有问题了。
这是我的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

最佳答案

您通过让两个对象相互引用来创建循环引用,因此您不应该使用唯一指针。尝试使用弱指针。

How to break shared_ptr cyclic reference using weak_ptr

关于c++ - 由于具有循环引用的类中的 unique_ptr 或 vector 而导致核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368506/

相关文章:

c++ - 从 unique_ptr<T> 的 void* 转换为 T** 是如何工作的?

c++ - 遍历 unique_ptr 的容器

C++:尝试理解通过引用传递 vector

c++ - TCP:如何生成 seq/ack 号?

c++ - 实现交换/复制功能 : is there a better way?

c++ - 尝试在 vector 类中创建调整大小函数

c++ - 如何避免一直写 std::reference_wrapper 版本模板?

c++ - STL vector 和 c++ : how to . 在没有默认构造函数的情况下调整大小?

java - 使用java中Vector的indexOf()方法来匹配一个成员变量

c++-cli - 在 header 中声明 unique_Ptr 变量,然后在构造函数中稍后分配它的语法是什么?