- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试按照给定的说明解决大学问题。代码可以编译,但会产生有关内存的警告。这是代码的关键:
#include <iostream>
#include <vector>
#include <memory>
using std::vector;
using std::shared_ptr;
using std::make_shared;
class ApsClass {
protected :
double goose;
public :
virtual ~ApsClass() {}
virtual shared_ptr<ApsClass> clone() const = 0;
double GetGoose() const { return goose; }
};
class DerivedClass : public ApsClass {
public :
~DerivedClass() {} // Warning here*
DerivedClass(double goose) { DerivedClass::goose = goose; }
shared_ptr<ApsClass> clone() const override;
};
shared_ptr<ApsClass> DerivedClass::clone() const {
return make_shared<DerivedClass>(goose);
}
class Storage {
vector<shared_ptr<ApsClass>> geese;
public :
Storage() {}
ApsClass *AddApsClass(ApsClass *pok);
void DelApsClass(ApsClass *pok);
};
ApsClass *Storage::AddApsClass(ApsClass *pok)
{
// The instructions stated that the object pointed at by pok has no
// other pointers pointing at it, and should now be "owned" by geese
geese.push_back(shared_ptr<ApsClass>(pok));
return pok;
}
void Storage::DelApsClass(ApsClass *pok)
{
for (int i = 0; i < geese.size(); i++)
if (geese[i] == shared_ptr<ApsClass>(pok)) {
geese.erase(geese.begin() + i);
break;
}
}
int main ()
{
Storage Foo;
ApsClass *s = Foo.AddApsClass(new DerivedClass(0.5));
Foo.DelApsClass(s);
return 0;
}
main() 基本上是来自自动测试的代码,我应该修改程序的其余部分以使 main() 正常工作。
该警告在派生类*的析构函数定义中报告,翻译成英文时它只是说“Bad deallocation”。我的问题是:代码中哪里以及为什么会出现这个问题?
我删除的代码对内存分配没有影响(我希望如此),并且是通过自动测试进行测试的,我没有太多访问权限。一项测试报告了以下内容:
Invalid read of size 8 ...
Address 0x51f0090 is 0 bytes inside a block of size 56 free'd
和
==10216== Jump to the invalid address stated on the next line
==10216== at 0x0: ???
Address 0x0 is not stack'd, malloc'd or (recently) free'd
和
==10216== Process terminating with default action of signal 11 (SIGSEGV)
==10216== Bad permissions for mapped region at address 0x0
==10216== at 0x0: ???
如果需要更多详细信息,我可以将一些原始代码放在这里。
最佳答案
每次你做shared_ptr<ApsClass>(pok)
您正在创建一个新的 shared_ptr
不知道所有其他shared_ptr
s 指向同一个对象。所以当这个shared_ptr
被销毁它会销毁对象。
但是因为你做了几次(在你的例子中是两次)对象被破坏了不止一次。
只有 shared_ptr
从普通指针创建的 s 有这个问题。创建 shared_ptr
来自另一个shared_ptr
很好。
您应该创建一个 shared_ptr
一次 - 改变这个
ApsClass *s = Foo.AddApsClass(new DerivedClass(0.5));
Foo.DelApsClass(s);
为此:
shared_ptr<ApsClass> aps(new DerivedClass(0.5));
shared_ptr<ApsClass> s = Foo.AddApsClass(aps);
Foo.DelApsClass(s);
并更改ApsClass *
至 shared_ptr<ApsClass>
其他地方。
关于c++ shared_ptr继承内存问题 "Bad deallocation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44751489/
使用 shared_ptr 时,我应该只使用 shared_ptr 吗?申报一次或申报shared_ptr无论我经过哪里? 所以在我新建实例的函数中,我将它包装在 shared_ptr 中但是当我从函
#include #include #include using namespace std; struct Node { Node(int data, boost::shared_pt
对于我目前正在处理的代码,我们有时需要使用较旧的编译器在一些较旧的系统上进行编译(例如,我们在较旧的 IBM BlueGene/L 上运行 sims,其支持契约(Contract)规定了一些非常旧的
我正在阅读 this answer作者指的是boost best practices其中说: Avoid using unnamed shared_ptr temporaries to save ty
我正在处理几个类,我想知道如何在我的应用程序类中使用普通成员,而该成员需要使用 shared_from_this()? 这里有一些代码来阐明我的意思(见评论) class Observable { p
我有一个 Foo 类,其中包含一个 Hotel 类 的 shared_ptr,以及一个 Rules 类(位于 namespace Rules 内): class Foo { public: //
我想不通。看起来我遗漏了一些简单的东西?我要在 MakePointToSameValue 中输入什么,以便在点 (1) b.ptr 和c.ptr 与a.ptr 指向同一个 换句话说,a.ptr.get
我已尽我所能制作了 SSCE。我怀疑共享指针在我在 main 中请求它们之前解构(释放)了我的对象。如何在不完全绕过共享指针的情况下防止这种情况发生?这是一个程序中的孤立问题,否则可以通过使用 sha
这个问题在这里已经有了答案: Set shared_ptr with new_pointer that is old_pointer + offset (1 个回答) 关闭 4 年前。 我目前正在学
假设我们有一个类,成员如下 std::map> member_我们无法替换 member通过具有 std::shared_ptr 的 map ,因为该类必须对 ObscureType 的非常量函数进行
我正在用 C++ 做学校作业(我还在学习)。我正在尝试实现随机生成的二叉树结构,使用 shared_ptr 在多个地方存储节点的信息(我需要它作为作业)。考虑以下示例代码(这是我的小测试程序): #i
我有以下类(class) struct Images { std::vector > ptr_vector; } 将 ptr_vector 放入 std::shared_ptr 中不会在复制
我刚刚对一个项目进行了大规模重构,添加了一个基类来代替现在所说基类的派生类(因为我想要这个类的更多“类型”)。 我的问题是,一些实用函数将原始类 A 的引用作为 shared_ptr,因此函数声明如下
我记得 Scott Meyers 教我的 func(shared_ptr(new P), shared_ptr(new Q)); 是危险的,因为(如果我没记错的话)内存分配、引用计数(构造)和分配给
给定 struct X { void f(std::shared_ptr); }; auto x(std::make_shared()); 我大概可以安全地做 x->f(std::move(x
我试着介绍了一些const一些新代码的正确性(实际上是功能范例),发现我无法传递 std::shared_ptr到一个需要 std::shared_ptr 的函数.请注意,我不想放弃 constnes
我需要将原始指针包装到 shared_ptr 中,以便将其传递给函数。该函数在返回后不保留对输入对象的任何引用。 { MyClass i; shared_ptr p(&i); f(p);
我在继承链中有4个类:A-> B-> C,A-> B-> D,其中B是唯一的类模板。 我想拥有一个在id和对象指针(C或D)之间映射的std::map,但是我在将make_shared输出分配给std
boost::shared_ptr 是否解决原始指针问题? Base* p = new Base(); shared_ptr sp(p); shared_ptr sq(p); 两个 shared_
How can shared_ptr be a subclass of shared_ptr? 我想知道如何实现模板类 C这样 C是 C 的子类? 我已经观察到上述情况,例如 shared_ptr和
我是一名优秀的程序员,十分优秀!