When it comes to manually managing memory, it's absolutely fantastic to have coding patterns for making your memory management duties...well...manageable. Reference counting is one such pattern that is widely used throughout computer programming. COM (which is on top of C++) uses it extensively, and thus the underlying system of memory management in C# uses it, but so does Core Foundation (C level API on Mac OS X and iOS) and, even more noticeably, is that the Objective-C language has the reference counting memory management pattern built in.
At this point, I assume everyone knows what reference counting is and how to use it. If not, you should verse yourself with
Apple's Memory Management Overview. Now since Apple has introduced
Automatic Reference Counting (ARC), Objective-C memory management has reached near trivial levels as that of managed memory languages like Java and C#. But for those of us that need to support older platforms, work in legacy code bases or develop public libraries that require non-ARC support, being elegant with reference counting is very important. And even those of us that are converted over to ARC, we still need to be aware of Core Foundation memory management which does not have ARC.
So that was a serious introduction to nothing it would seem! Well, what this post will actually cover is the pitfall of recursive (aka reentrant) code that can lead to crashing in a ref counted world. Basically, we'll deal with releasing an object that leads to it being released again.
The problem: reentrant releases!
CodeProjectRead more ยป