[Issue 15513] Memory Corruption with thread local objects

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jan 20 10:44:41 PST 2016


https://issues.dlang.org/show_bug.cgi?id=15513

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g at gmail.com

--- Comment #5 from ag0aep6g at gmail.com ---
Reduced this some:

foo.d:
----
import std.stdio: writeln;
import core.thread: Thread;

void main() {
  writeln("Start frop from D");
  frop();
}

extern(C) void function() startup_routine = &initialize;

extern(C) void initialize() {
  import core.runtime;  
  Runtime.initialize;
  writeln("Start frop from C");
  frop();
  Runtime.terminate();
}

void frop() {
  Thread bar = new Thread(&foo);
  bar.start();
  bar.join();
}

void foo() {
  proc();
  new int;
  new int[10_000];
  writeln(dash);
}

int[] dash;

void proc () {
  dash.length = 8;
  dash[] = 0;
}
----

main.c:
----
#include <dlfcn.h>

typedef void (*routine_t)(void);

int main(int argc, char*argv[]) {
  void* dll = dlopen("./foo.so", RTLD_LAZY);
  routine_t* routine = (routine_t*)dlsym(dll, "startup_routine");
  (*routine)();
}
----

Compile and run:
----
dmd foo.d && ./foo

# change the path for the -L-R option according to your setup
dmd -fPIC -shared -offoo.so -L-ldl -L-lphobos2 -L-R$HOME/d/dmd2.git/linux/lib64
foo.d
gcc -m64 -fPIC main.c -ldl -o main
./main
----

Output:
----
Start frop from D
[0, 0, 0, 0, 0, 0, 0, 0]
Start frop from C
[-1488654016, 32761, 37363216, 0, 0, 0, 0, 0]
----

--


More information about the Digitalmars-d-bugs mailing list