On 24 August 2012 14:54, Jan van de Sandt <jvdsandt@gmail.com> wrote:
Hello,
I have a subclass of NBExternalObject to refer to an external object (from the ICU library). When I'm done with the object I need to close it. The following instance method takes care of this:
primClose <primitive: #primitiveNativeCall module: #NativeBoostPlugin> self nbCall: #( void ucal_close( ICUCalendarNB self ) )
This works fine. But now I want to make use of the NBExternalResourceManager so that external objects are closed automatically when they are no longer referenced. For this I have implemented the required instance method:
resourceData
^ handle
And the class methods:
finalizeResourceData: aHandle
Transcript show: 'Closing ', aHandle printString ; cr. aHandle isNull ifFalse: [ self primClose: aHandle ]
and
primClose: aHandle <primitive: #primitiveNativeCall module: #NativeBoostPlugin> ^ self nbCall: #( void ucal_close( void* aHandle ) )
But now two things are going wrong: - The #finalizeResourceData: doesn't seem to get called
perhaps because you still got a strong references to that object somewhere?
- If I call the #primClose: directy with the handle as an argument my Image crashes.
Ok it looks like a typical mistake, (which i also do from time to time ;) .. by passing a pointer to value instead of value itself. Here, a handle is a variable-byte object, holding a value (4 bytes) so, if you pass it as argument of following type: void* aHandle you will actually not pass the value of handle itself, but a pointer to a first byte in memory where that value held. To fix that try following: resourceData ^ handle value primClose: aHandleValue <primitive: #primitiveNativeCall module: #NativeBoostPlugin> ^ self nbCall: #( void ucal_close( uint aHandleValue ) )
Is the signature of the #nbCall: message wrong?
Jan.
PS: I'm using the latest NB-Cog-VM from https://ci.lille.inria.fr/pharo/view/Cog/
-- Best regards, Igor Stasenko.