How to avoid invalid memory operation errors (and more) in DLLs?

Heinz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 25 16:40:44 PST 2014


Hello everyone,

I'm making a big framework with D2 (DMD 2.066.1) and I've been
encountering many errors related to threads in DLLs.

My program flow is a main exe wich statically loads a main dll,
then this main dll dynamically loads an extra dll and then this
extra dll dynamically loads a final dll. Inside this flow i only
create 2 threads for a total of 3 including the main thread.
Using the included debugger in DMD, it shows that 5 threads are
created but i can't understand why the extra 2 are spanned.

The problem is that in the final dll whenever i make calls to
functions exclusive to this dll i get "invalid memory operation"
errors. I don't know if this is TLS related but i have __gshared
everywhere in my framework and compiling with -vtls shows no tls
globals.

While trying to debug the program i found a bug (i think) present
in all DMD2 latest versions: I named all threads so i could
identify them in the debugging process by assigning the
core.thread.Thread.name property. But in the already mentioned
final dll, whenever i try to get the core.thread.Thread.name
property to print it, the whole program crashes.
Luckily i was able to reproduce this bug by making a very simple
and scaled down demo. It's flow is similar to my real program: A
main exe statically loads a main dll and then the main dll
dynamically loads an auxiliary dll from wich i try to print the
thread's name but the whole demo crashes.

Here's the test case i made, it includes binaries and source
code:
https://dl.dropboxusercontent.com/u/2351331/dll-tls%20test_case.zip

Just run the program and type the word "magic", it will load up
to the auxiliary dll wich will try to print the current thread
name but it will crash.
If anyone with more experience in DLL matters could confirm if
the above behavior is indeed a bug i'll be greatly appreciated.

Finally, if possible, i'd like to receive suggestions on how to
avoid extra threads from being created and avoid memory operation
errors in DLLs.

Thank you guys for your time (and brains) and i hope you had a
great x-mas.


More information about the Digitalmars-d-learn mailing list