由于具有循环引用的类中的unique_ptr或向量而导致核心转储

Core-dumped due to unique_ptr or vector in classes with cyclic references

本文关键字:向量 转储 核心 ptr unique 循环 于具 引用      更新时间:2023-10-16

我正在尝试使用带有循环引用类的代码进行处理。当我试图将它们定义为类成员时,我发现我在unique_ptr或向量方面遇到了一些问题。

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

首先,我通过一个名为 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;
}

在启动类中,声明派生类(请注意基类不是这个):

//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类直接知道每个 orther 的公共成员。但是,在Derived_B类中添加unique_ptr或向量后,我有时会遇到核心转储的问题。

我已经用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类中添加新的向量成员后,类似的问题又出现了。此外,此问题发生在析构函数之前。我可以展示瓦尔格林德测试的一部分:

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 
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

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

如何使用weak_ptr断开循环引用shared_ptr