Pointers in extern DLL functions

BCS ao at pathlink.com
Sat Dec 22 12:47:37 PST 2007


Reply to Xavi,


> Oops!, one more thing; I've read this in the manual of D: "Undefined
> behavior: Do not store pointers into non-pointer variables using casts
> and other tricks."
> 
> Then in this code
> 
> SendMessageW(hwnd, 0, 0, cast(uint)toUTF16z(file));
> 
> the GC will not have any reference of the pointer returned by
> toUTF16z(), right?
> 
> I assume that if I run this code in a D multithreaded application
> another thread could call the GC collecting it while the
> SendMessageW() is running. Is this correct?
> 
> Cheers, Xavi
> 

I think you would be safe for the time being. As pointed out before the pointer 
would still be sitting on the stack. The thing about casts might be related 
to the fact that the GC keeps track of pointer-containing and non-pointer-containing 
heap allocated blocks. The thing about "other tricks" relates to things like 
XORing the left and right pointer in a doubly linked list (http://en.wikipedia.org/wiki/Xor_linked_list). 
To be pedantic, the GC could be changed so that the cast might make a difference, 
but I would be surprised if that happened any time soon.




More information about the Digitalmars-d-learn mailing list