On 24 August 2012 15:55, Jan van de Sandt <[email protected]> wrote:
> Hello Igor,
>
> Thanks for the explanation. Closing via the class method now works fine!
>
> But I still notice that the #finalizeResourceData doesn't get called. The
> instance is already gone because the #allInstances method returns an empty
> collection. Strange ....
>
btw did you added a call to #registerAsExternalResource
in instance initialization method somewhere (once you have initialized
handle of course)?
because of course, if you don't register it, a finalization will never
be called.


> Jan.
>
> PS: I installed NB in a new 1.4 image this afternoon. Some tests failed
> because the Integer>>#& method was missing. Probably is should be part of
> the AsmJit-Extension package. After I added this method the tests worked
> again.
>
>
>
> On Fri, Aug 24, 2012 at 3:38 PM, Igor Stasenko <[email protected]> wrote:
>>
>> On 24 August 2012 14:54, Jan van de Sandt <[email protected]> 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.
>>
>



-- 
Best regards,
Igor Stasenko.

Reply via email to