|
From: | Mathias Bauer |
Subject: | Re: Interesting difference in memory management of MacOS Foundation and GNUstep |
Date: | Thu, 27 Feb 2014 10:38:09 +0100 |
User-agent: | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 |
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;If you also want to build that on the Mac, this code will not compile, because the black magic from Apple not only protects the retain count but also forbids creating weak pointers to objects that are going to be deallocated. In that case you will need to create the code block using the weak pointer in your init routine and make the block an ivar of your class. This is how I solved the problem in my port of the GCDAsyncSocket.
Regards, Mathias
[Prev in Thread] | Current Thread | [Next in Thread] |