Segmentation fault while creating a class object by dlsym-ed function

Konstantin J. Chernov kjchernov at commander.pp.ru
Wed Jul 18 04:15:22 PDT 2012


Thank you for your replies!

I've found a working solution - all I needed is to change wstring 
to const wstring, and pass it not as func("something"), but as

   wstring tmp = "something"; func(tmp);

That's really odd, because I don't understand, how those changes 
made segfault disappear.

Here's the working code:

test.d:

import std.c.linux.linux;
import std.stdio;
import testclass;

int main(string[] args)
{
     void* handle = dlopen("./testclass.so", RTLD_LAZY | 
RTLD_LOCAL);
     testclass function(const wstring) a;
     a = cast(testclass function(const wstring))dlsym(handle, 
"loadClass");
     wstring tmp = "Test";
     testclass b = a(tmp);
     return 0;
}

testclass.di:

class testclass
{
     this(const wstring loadmsg);
     ~this();
     wstring foo();
};

testclass.d:

import std.stdio;

class testclass
{
     private wstring msg;
     this(const wstring loadmsg)
     {
         writeln("Class constructor");
         this.msg = loadmsg;
     }
     ~this()
     {
     }
     wstring foo()
     {
         return msg;
     }
};

extern(C) testclass loadClass(const wstring loadmsg)
{
     return new testclass(loadmsg);
}


dmd test.d -L-ldl
dmd testclass.d -fPIC -shared


More information about the Digitalmars-d-learn mailing list