void* pointers get corrupted: D bug or misunderstanding?

Federico Santamorena federico at santamorena.me
Sun Jul 28 18:32:24 UTC 2019


Hello, I finally write on the forum for the first time to finally 
find the answer to a problem I am having:

I am calling native GTK with extern(C) function declarations 
instead of using gtkD, mainly because I just need very few 
functions and I don't want a big library that would take about 
20x time to compile than my project with very few binds.

And I either found a D bug or a lack in documentation about 
extern(C) or this is a very specific case that doesn't happen 
often.

I believe this is the same "bug" as this:
https://forum.dlang.org/thread/fjfftrruedmzdcqmrbci@forum.dlang.org

So the problem I am noticing is very simple: as you may know GTK 
callbacks have a void* user_data argument, what happens, is that 
if the flow of code is this: D => C => D the void* pointer when 
reaching the third step and finally emerging to the D language 
will change its value and get corrupted.

A stupid example can be passing a D object using the void* 
user_data GTK argument, passing a extern(C) callback function to 
GTK, and then inside that callback calling a extern(D) function 
that accepts a void*.

I already tried using Variant, but pointers to Variant get 
corrupted too, so it's useless.

I tested this with Valgrind and there are no stack corruptions, 
GDB confirms that any void* pointer doing the D => C => D route 
gets corrupted (changes its value to a random(?) one) in the 
final step.

The TL;DR is: when the flow of code is inside an extern(C) 
function with a D object passed as a void* pointer it seems there 
is no way to pass that object again to another D function 
accepting a void* pointer

Additional Notes:
Tried LDC LLVM and it still happens, so it's not a DMD bug




More information about the Digitalmars-d mailing list