Re: [Pharo-users] [Vm-dev] Fwd: Need help with Alien, unexpected function return value
Steve,
result := (Alien newGC: 4) pointer. functionPointer primFFICallResult: result with: self asJNIParameter. ^result signedIntAt: 1
[snip] When you use a pointer alien (which has a size field of zero) as the return result of a function call, the alien code expects the result from the alien to be a pointer to some area of memory and replaces the address field of the result alien with the result returned by the function call. You're lucky your method returned any result at all rather than crashing with an access violation - you are dereferencing whatever sits at 0x00010006. [snip]
result := (Alien newC: 4). functionPointer primFFICallResult: result pointer with: self asJNIParameter. ^result signedIntAt: 1
[snip] This time you are returning the signed int at the block of memory allocated by the newC: call. The memory gets zeroed before use, which is why you get a zero. Because you used "result pointer" in the alien call the alien code again replaces the pointer alien's address with the result. This time though, you don't have a reference to the pointer alien, so the result is effectively discarded.
Thanks a lot for the explanation! Am 16.05.10 03:58 schrieb Steve Rees:
Try the following
result := (Alien new: 4). functionPointer primFFICallResult: result with: self asJNIParameter. ^result signedIntAt: 1
Yes, that's it. I get the expected result now. I'll have to inspect the rest of my (ab)use of Aliens now, but that shouldn't take long. After that I'll have to find out where I should explicitly free Aliens and where the garbage collector will do that for me. I am sure I have built in some nice memory leaks. :-) Thanks again! Joachim
participants (1)
-
Joachim Geidel