www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialization sequence of runtime environment

I recently posted another thread (regarding crt1.o: could not 
read symbols: Bad value - I'm creating a new thread because it's 
another problem) and I figured out that the following error:

stack_bottom = 7fc98804ae18 thread_stack 0x40000
/usr/sbin/mysqld(my_print_stacktrace+0x35)[0x8cea15]
/usr/sbin/mysqld(handle_fatal_signal+0x4a4)[0x65e0d4]
/lib64/libpthread.so.0(+0xf710)[0x7fc98abcb710]
/usr/lib64/libphobos2.so.0.65(gc_malloc+0x29)[0x7fc951a70e05]

... occures when I never call rt_init()/rt_term() (or 
Runtime.initialize()/Runtime.terminate()). That solved the 
problem and I can successfully execute all my D-code.

I'm using the shared library as UDF in MySQL. My current code 
looks as follows:

export extern(C){
	my_bool mysql_d_udf_init(UDF_INIT *initid, UDF_ARGS *args, char 
*message)
	{
		return (rt_init() ? 0 : 1);
	}
	
	void mysql_d_udf_deinit(UDF_INIT *initid)
	{
		rt_term();
	}
	
	char* mysql_d_udf(UDF_INIT* initid, UDF_ARGS* args, char* 
result, c_ulong *length, char* is_null, char* error)
	{
		string res = "Hello World!";
		result = cast(char*) res.ptr;
		
		*length = res.length;
		
		return cast(char*) result;
	}
}

When I register the function in MySQL and execute "select 
mysql_d_udf()" everything works fine. I'm receiving "Hello 
World!" and everything is fine. But when I try to restart the 
server, the server is not responding. I've to kill the server 
manually which is impracticable in productional environment.

I first thought that rt_term() throws an exception (or something 
else) that prevents the MySQL server from being restartet 
gracefully. So I removed rt_term() from my deinit()-function. But 
that didn't solve the problem. When I also remove rt_init() I'm 
getting the gc_malloc-exception as described above. So it seems 
that rt_init() registers/enables something I'm unable to release 
I don't know exactly. Fact is that I see no exception or log 
entry in my MySQL log and the server restarts successfully 
without my D-based UDF plugin. So something seems to be wrong...

I don't know if my initialization of the D-runtime environment is 
correct? I sometimes read something about gc_init() (but I 
thought rt_init()/Runtime.initialize() handles that for me...). 
Any suggestions how to initialize/terminate the runtime 
environment in shared libraries? Probably I'm doing something 
wrong...

If there's no solution, I've to write the UDF functions using C 
(I don't prefer that way because all other projects are D-based).
Jun 13 2014