What do you think of this code in Objective-C that iterates to save the count and cause the release of each iteration?

I am still trying to understand this piece of code that I found in the project I am working on when the guy who created it left the company before I could ask.

This is the code:

-(void)releaseMySelf{ for (int i=myRetainCount; i>1; i--) { [self release]; } [self autorelease]; } 

As far as I know, in the Objective-C memory management model, the first rule is that an object that allocates another object is also responsible for its future release. This is the reason why I do not understand the meaning of this code. Is there any reason?

+8
memory-management objective-c retaincount autorelease
source share
4 answers

The author tries to do without understanding memory management. He assumes that the object has a save account, which increases by each save and therefore tries to reduce it by triggering this number of releases. He probably didn’t realize "he is also responsible for his release in the future." part of your understanding.

However, see many answers here, for example. here and here and here .

Read Apple Memory Management Concepts .

The first link includes a quote from Apple

The saveCount method does not take into account pending messages sent to the recipient.

Important: This method usually does not matter in the debug memory of a control problem . Since any number of infrastructure objects can have a saved object for storing links to it, and at the same time, abstract pools can contain any number of pending releases on the object, it is very unlikely that you can get useful information from this method . To understand the basic rules of memory management that you must follow, read "Memory Management Rules." Use the appropriate tool to diagnose memory management problems: The LLVM / Clang Static analyzer can usually find memory management problems even before running your program. Object Alloc tool in the Tools application (see the Tools User Guide). placement of objects and their destruction. Shark (see the Shark User Guide) also profile memory allocations (among many other aspects of your program).

+17
source share

Since all answers seem wrong for myRetainCount as [self retainCount], let me suggest a reason why this code could be written: maybe this code somehow spawns threads or otherwise registers clients, and that myRetainCount is actually the number of these clients , is stored separately from the actual amount of OS residues. However, each client can also maintain their ObjC style.

Thus, this function can be called when the request is interrupted and can immediately get rid of all clients at the same time, and then complete all releases. This is not a good design, but if it works with the code (and you did not leave int myRetainCount = [self retainCount] or redefined storage / release), at least it is not necessarily a buggy.

This, however, is very likely a poor distribution of responsibilities, or kludgey and battered attempts to avoid maintaining laps without improving anything.

+7
source share

This is a dirty hack to force free memory: if the rest of your program is written correctly, you will never have to do anything like that. Usually your holdings and releases are in equilibrium, so you never need to look at your retention account. What this code snippet says: "I don’t know who saved me and forgot to release me, I just want my memory to be freed, I don’t care that links to others will hang down from now on." This is not going to compile with ARC (oddly enough, switching to ARC can only fix the error that the author was trying to work with).

+3
source share

The meaning of the code is to make the object free right now, no matter what the future consequences may be. (And there will be consequences!)

The code is deadly wrong because it does not take into account the fact that someone actually "owns" this object. In other words, something “distributes” this object, and any number of other things can “save” this object (maybe a data structure like NSArray, maybe an autostart pool, maybe some code on the stack that just does a "save",); all of these things share property in this property. If an object commits suicide (this is what releaseMySelf does), these “owners” suddenly indicate a bad memory, and this will lead to unexpected behavior.

Hopefully the code written like this will just crash. Perhaps the original author avoided these crashes, a memory leak elsewhere.

+2
source share

All Articles