discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interesting difference in memory management of MacOS Foundation and


From: David Chisnall
Subject: Re: Interesting difference in memory management of MacOS Foundation and GNUstep
Date: Thu, 27 Feb 2014 11:02:39 +0000

On 27 Feb 2014, at 09:38, Mathias Bauer <mathias_bauer@gmx.net> wrote:

> 
> 
> Am 27.02.14 06:58, schrieb Eric Wasylishen:
>> Hey David,
>> 
>> I ran in to the same problem Mathias mentioned. It's easy to hit if
>> you're managing some resource with GCD, and that resource needs to be
>> closed in -dealloc (in my case, it's the database connection in
>> CoreObject that I only access within a particular dispatch queue, and
>> I need to close the connection in -dealloc).
>> 
>> You can trigger the failure just by using a block in dealloc that
>> causes self to be retained:
>> - (void) dealloc
>> {
>>     void (^myBlock)() = ^() {
>>         id foo = self;
>>         NSLog(@"inside myBlock, foo = %p", foo);
>>     };
>>     myBlock();
>> }
> 
> In case you are only building this on Linux, but not on Mac, you can fix it 
> by using a weak pointer:
> 
> MyClass* __weak this = self;
> void (^myBlock)() = ^() {
>       id foo = this;

I've committed both of these as test cases in r37717, and they're now both 
working.  The latter version (with a weak pointer) crashes on OS X, but just 
sets this to nil for us now.  Note that the correct way of doing this is most 
likely to be __unsafe_unretained, which gives you a reference to the object 
without poking its reference count, and is really the only safe way of doing it 
in -dealloc.  

David

--
This email complies with ISO 3103




reply via email to

[Prev in Thread] Current Thread [Next in Thread]