SEGFAULT when converting C string to string
    DFTW 
    jckj33 at gmail.com
       
    Mon Mar 18 22:20:35 UTC 2019
    
    
  
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;
}
if matters, compiled with dub: "targetType" : "dynamicLibrary"
on 64-bit machine.
This is the piece of C code, foo.c:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(void)
{
   printf("+main();\n");
   const char* libpath = "mylib.so";
   void *lh = dlopen(libpath, RTLD_LAZY);
   if(!lh) {
     fprintf(stderr, "dlopen error: %s\n", dlerror());
     return EXIT_FAILURE;
   }
   int (*fn)(const char*, int) = dlsym(lh, "foo");
   char *err = dlerror();
   if(err) {
     fprintf(stderr, "dlsym error: %s\n", err);
     return EXIT_FAILURE;
   }
   const char *name = "jhon";
   int age = 18;
   int result = (*fn)(name, age);
   printf("unloading lib");
   dlclose(lh);
   printf("result = %d\n", result);
   printf("-main()\n");
   return 0;
}
compile as:
gcc foo.c -ldl
My OS is Ubuntu version 18
I'm assuming const char* both in C and D are raw pointers.
    
    
More information about the Digitalmars-d-learn
mailing list