Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Better Reference Counting (Safe Release)

, 4 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Better reference counting.
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!


Read more »

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

NSProgrammer
Technical Lead
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150128.1 | Last Updated 4 Mar 2013
Article Copyright 2013 by NSProgrammer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid