[英]Vector of pointers to dynamically-allocated objects: what could go wrong?
I checked out this thread before posting here: How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?我在此处发布之前检查了此线程: How to Avoid memory leaks when using a vector of pointers to dynamic allocation objects in C++?
Basically, I have some vectors of pointers that point to dynamically-allocated objects:基本上,我有一些指向动态分配对象的指针向量:
vector<MyType*> a;
a.push_back(new MyType());
These vectors are private instance variables of a few classes I'm writing, and the dynamically-allocated objects are destroyed in the classes' destructors by iterating thru the vectors and calling delete
on each pointer.这些向量是我正在编写的几个类的私有实例变量,动态分配的对象在类的析构函数中被销毁,方法是遍历向量并在每个指针上调用
delete
。
It all works fine most of the time, but every once in a while a pointer in one of these vectors becomes invalid and causes a segfault
when my code attempts to use it.大多数情况下一切正常,但每隔一段时间,这些向量之一中的指针就会变得无效,并在我的代码尝试使用它时导致
segfault
错误。 I'm having trouble figuring out why these pointers occasionally break.我无法弄清楚为什么这些指针偶尔会中断。
Are there any reasons why the dynamically-allocated object's address would change and cause the pointers to become invalid?动态分配对象的地址是否会发生变化并导致指针无效?
I can try to post actual code if necessary.如有必要,我可以尝试发布实际代码。
EDIT:编辑:
Ok I have a number of things going on here.好的,我这里有很多事情要做。 There are two custom classes:
VisaLane
and VisaResource
.有两个自定义类:
VisaLane
和VisaResource
。 VisaLane
contains a vector<VisaResource*>
of pointers to VisaResources created using new VisaResource()
. VisaLane
包含指向使用new VisaResource()
创建的 VisaResources 的指针vector<VisaResource*>
。
Each VisaLane
is also created using new VisaLane()
whose pointers are stored in a vector<VisaLane*>
.每个
VisaLane
也是使用new VisaLane()
创建的,其指针存储在vector<VisaLane*>
中。
This is one example of how the pointer gets corrupted.这是指针如何损坏的一个示例。 Item 0 in the vector has inaccessible members:
向量中的第 0 项具有不可访问的成员:
resources_ <3 items> std::vector<VisaResource*>
[0] VisaResource
function_ <not accessible> std::string
name_ <not accessible> std::string
state_ VisaResource::FREE VisaResource::VisaResourceState
value_ 6998928 uint
[1] VisaResource
function_ "lane_clksel" std::string
name_ "m1_lane0_clksel" std::string
state_ VisaResource::FREE VisaResource::VisaResourceState
value_ 0 uint
[2] VisaResource
function_ "lane_bypass" std::string
name_ "m1_lane0_bypass" std::string
state_ VisaResource::FREE VisaResource::VisaResourceState
value_ 0 uint
visa_res_itr __gnu_cxx::__normal_iterator<VisaResource**, std::vector<VisaResource*>>
You should not use raw pointers at all, Best way to avoid this is to use Smart pointers , Unique_ptr
if you are not sharing your vector contents of shared_ptr
if container elements are being shared across multiple enity's.您根本不应该使用原始指针,避免这种情况的最佳方法是使用Smart pointers ,
Unique_ptr
如果您不共享shared_ptr
的矢量内容,如果容器元素在多个实体之间共享。
Using smart pointers will most likely help you get rid of your problem.使用智能指针很可能会帮助您摆脱问题。
Other than that without seeing the source code, we can't comment on what is really going wrong.除此之外,没有看到源代码,我们无法评论真正出了什么问题。
You can make your own copyable reference counted smart pointer class for STL containers or use std::unique_ptr
if your compiler supports C++0x.您可以为 STL 容器制作自己的可复制引用计数智能指针 class,或者如果您的编译器支持 C++0x,则使用
std::unique_ptr
。 If it's slightly older you may have something in the std::tr1
namespace.如果它稍微旧一点,你可能在
std::tr1
命名空间中有一些东西。
Alternately consider std::vector<MyType>
as that container allocates contiguously on the heap anyway and will manage the memory for you.或者考虑
std::vector<MyType>
因为该容器无论如何都会在堆上连续分配,并将为您管理 memory 。 Avoid raw pointers at all cost.不惜一切代价避免使用原始指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.