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

Konstantin J. Chernov kjchernov at commander.pp.ru
Tue Jul 17 21:51:59 PDT 2012


Hello. When I'm trying to create a class object by using a 
dlsym-ed function, I'm getting a segmentation fault after 
execution of program.

However, if I'm deleting that object before 'return 0' in main 
(by using 'delete b'), everything is going fine.

I'm just started to learn D after using C++ for more than 5 
years, so some things look really strange for me (especially that 
'new className()' doesn't return a pointer).

What am I doing wrong here? Is there any way to do what I'm 
trying to do right way?

Thanks.

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(wstring) a;
     a = cast(testclass function(wstring))dlsym(handle, 
"loadClass");
     testclass b = a("test");
     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);
}



More information about the Digitalmars-d-learn mailing list