超奇怪的问题触发"分段错误" [英] Super-weird issue triggering "Segmentation Fault"
问题描述
我是不会走的很深入的问题(在codeBase的已经是千行和相当复杂的),所以我会尽力miniminise了...窗口,以我所发现。
I won't go very deep into the issue (the codebase is already thousands of lines and quite complex), so I'll try to miniminise the... "window" to what I've spotted.
这里的例行触发段错误:
extern (C)
{
void* Statements_new() { return cast(void*)(new Statements()); }
void Statements_add(Statements s, Statement st)
{
//writeln("In here");
if (s is null) writeln("StatemenTS are null");
else writeln("not null : "~ typeid(s).name);
if (st is null) writeln("statement is null");
else writeln("not null : " ~ typeid(st).name);
s.add(st);
//writeln("Out of here");
}
}
的几个注意事项:
- 申报的方法是什么,但绑定,使原生程序可以直接从C code(野牛实际上)被调用。
- 的
Statements_add
函数被调用了语句
对象和子类语句
对象。
- The declared methods are nothing but "bindings" so that native routines can be called directly from C code (Bison actually).
- The
Statements_add
function is called with aStatements
object and a subclassedStatement
object.
现在,它的怪事:
- 错误不会发生所有的时间(实际上它不会发生像99%的时间),但是当它的
s.add(ST);
声明似乎是罪魁祸首。 - 永远不要为2参数之一(
取值
,ST
)空
。 - 现在,如果我评论了2
如果... ... writeln的typeid
语句,误差是存在的。 - 如果我取消它们(他们没有做任何事情,对吧?),它始终工作 - 固定 - !宾果
- The error doesn't happen all the time (actually it doesn't happen like 99% of the time), but when it does, the
s.add(st);
statement seems the culprit. - Never ever is one of the 2 parameters (
s
,st
)null
. - Now, if I comment the 2
if... writeln... typeid
statements, the error is there. - If I uncomment them (they don't do anything, huh?), it always works - fixed - bingo!
这是怎么回事???
一些更多的细节:
- 编译器: DMD64 D编译器v2.065
- 调试器: LLDB
- OS: OSX 10.9.2
- Compiler : DMD64 D Compiler v2.065
- Debugger : lldb
- OS : OSX 10.9.2
推荐答案
如果您通过在D中code分配从D堆非-D code对象的唯一的参考,那么你必须的任何注册为GC根,或者改变您的code到使用的malloc
,而不是从管理ð堆中分配。否则,GC会认为对象是未使用的,它收集来释放内存。
If you are passing the only reference of an object allocated in D code from the D heap to non-D code, then you must either register it as a GC root, or change your code to use malloc
instead of allocating from the managed D heap. Otherwise, the GC will think that the object is unused, and collect it to free memory.
这篇关于超奇怪的问题触发"分段错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!