SEGFAULT when converting C string to string

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Mar 18 22:33:21 UTC 2019


On Mon, Mar 18, 2019 at 10:20:35PM +0000, DFTW via Digitalmars-d-learn wrote:
> I'm writing a shared library in C to be used from a C program, so I
> went to my small tests. That one failed give a SEGFAULT on the
> std.conv.to call.
> 
> This is the pice of D code/the library:
> 
> extern(C) int foo(const char *name, int age)
> {
>     import core.stdc.stdio : printf;
>     import core.stdc.stdlib;
>     import std.conv : to;
>     printf("printf() got called! age = %d\n", age);
>     printf("name = [%s]\n", name);
>     //auto nm = to!string(name); // <-- everything works until I uncomment
> this line.
>     return age * 2;
> }
[...]

Most likely explanation: you failed to call rt_init() before using a
language feature that requires druntime to be initialized. In this case,
a GC allocation by std.conv.to!string.

Call rt_init() immediately after loading the library, and calling
rt_term() after you're done with it, should fix this problem.


T

-- 
Talk is cheap. Whining is actually free. -- Lars Wirzenius


More information about the Digitalmars-d-learn mailing list