Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Super-weird issue triggering "Segmentation Fault"

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.

Here's the routine triggering the "Segmentation Fault" :

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"); 

    }

} 

A few notes :

Now, the weirdness of it :

What's going on???


A few more details :

Upvotes: 2

Views: 163

Answers (1)

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

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.

Upvotes: 7

Related Questions