- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢您查看我的问题。
我有一个在我的程序中动态创建的对象。创建是循环的一部分,第一次迭代工作正常。
创建后,我的对象基类将自身添加到 map 中。
这是一些示例代码:
public class Base {
Base() {
// Add itself to a map
Data::objects[key] = this;
}
}
public class Derived : public Base {
// This ctor only initialize one int field.
Derived() : Base() {};
}
是不是很简单?
在我的代码中,我执行了 Derived * d = new Derived();
并且由于一些愚蠢的原因,我得到了一个 SIGSEGV。
我尝试调试它,但它在崩溃前甚至没有进入 ctor!
这是我的调用堆栈,因此您可以更好地帮助我:
地址:@0x002c0000
ntdll!RtlReleasePebLock()
地址:@0x0000000c 在 c:...\STL_deque.h:514
msvrct!malloc()
libstdc++-6!_Znwj()
fu87_ZSt4cerr(this=0xbc1ad8, e="//my object name//") at//my object name//.cpp
...其他是我的台词。
谢谢你,迈克尔
{享受}
编辑:添加关于 map 的信息
map 静态地位于数据类中。
// Data.h
class Data {
static map<int, Base*> objects;
}
// Data.cpp
#include "Data.h"
map<int, Base*> Data::objects;
// methods implementations
你怎么能破坏堆,我怎么能发现发生了腐败?
最佳答案
Data::objects
在创建 Base
的任何用法之前是否已初始化?
当你在最终链接目标中有多个翻译单元(读取,.cpp
文件)时,你不能保证类对象 objects
已经被初始化,除非您已付出特别努力来确保它。
大多数人通过使用静态类来解决这个问题,通过静态类可以保证在首次使用时进行初始化。像这样的东西:
// untested code, typed in by hand, not compiled through a machine compiler.
class Base {
public: static addObject(Base* that);
Base::Base() { Base::addObject(this); }
};
class Derived: public Base {
Derived::Derived() {}
};
//
// and in the .CPP for Base
namespace /* hidden */ {
int object_number = 0;
map<int,Base*> *objects = NULL;
}
void Base::addObject(Base* that) {
// TODO: do something to avoid multi-thread issues if that is ever a concern
if (!objects) {
objects = new map<int,Base*>();
}
(*objects)[++object_number] = that;
}
关于C++ new() 在调用 ctor 之前崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5541488/
我似乎遇到过一个例子,其中默认的移动构造函数似乎根据情况被视为用户声明的和非用户声明的: struct Foo { int a; Foo():a(42){}; //Foo(co
我在我的代码中定义了一个复制构造函数,它正在初始化正在创建的对象的数据成员。现在,如果我只需要更改几个变量的值,我正在编写一个新的复制构造函数。所以我的问题是,我是否可以只初始化特定的不同数据成员,而
为什么从 bar 返回时调用复制构造函数而不是移动构造函数? #include using namespace std; class Alpha { public: Alpha() { cout
我很抱歉这个晦涩的标题,不知道如何更好地表达它。 考虑以下继承层次结构: struct A { A(int) {} }; struct B : virtual A { B() : A(
为什么 var b = new B() 首先进入 static B() .ctor 而不是 static A () .ctor 而不是像实例构造函数那样反之亦然 (public A() 而不是 pub
我正在使用 SourceryGpp lite for arm 开发一个应用程序和一个库。 我没有使用标准库或默认启动文件。因此,要调用我正在对以下代码执行的全局 ctrs: ldr r0,=__cto
class Foo { public: Foo() { Foo(1)} Foo(int x, int y = 0):i(x) {} private: int i; } 任何人都可以给我
在下面的代码中,我想移动构造一个没有可用移动构造函数的对象: class SomeClass{ public: SomeClass() = default; SomeClass(con
这在 C++11 中似乎不起作用: class B : public A { public: B(const A& a) : A(a) // parent constr
我们如何在 F# 中为不可变结构定义一个仅接受部分字段的构造函数。或者,与 C# 相比,我们如何在 f# 中将结构清零(例如在下面的 c# 示例中调用 this())? c# struct Point
代码 #include using namespace std; #define PF cout class derp { public: derp() = default
我所拥有的基本上是一个 std::map,其中包含指向 Views 的指针。 std::map myViews; template bool addView( string assocName ,
我有下面的代码来测试std::string类的copy ctor和move ctor,结果让我吃惊,move ctor慢了~1.4倍比抄袭者。 据我了解, move 构造不需要分配内存,对于std::
我有一个静态构造器,它从我的配置服务器获取配置并设置一个静态变量。 我有一个常规构造函数,它根据该配置实例化一些属性。 这是我类(class)的一个例子: public class MyClass {
这个问题已经有答案了: How do I call New-Object for a constructor which takes a single array parameter? (2 个回答)
我有一个小问题,我不确定这是一个编译器错误,还是我这边的愚蠢。我有这个结构: struct BulletFXData { int time_next_fx_counter; int next_fx
如果我有一个同时定义了复制构造函数和移动构造函数的类,我是否需要使复制构造函数的参数const? 最佳答案 如果copy ctor取的不是const引用参数而是引用参数,那么你将无法copy构造con
我想知道是否有一种安全编程实践可以在这种微妙的行为发生时提醒编码人员,或者更好的是,首先避免这种行为。 struct A 的用户可能没有意识到没有 move 构造函数。在他们尝试调用不存在的 ctor
当类具有普通构造函数和/或普通析构函数时,C++ 标准定义了一些非常具体的行为。 例如,根据标准的 §3.8/1: The lifetime of an object of type T ends w
如果我想禁止复制构造/赋值那么是: class foo { public: foo(const foo&) = delete; foo& operator = (const foo&) =
我是一名优秀的程序员,十分优秀!