Search for who has an object hold counter

I have a UIViewController that has a keepCount of 3 points when I create it. This is so terribly wrong for me. What is the best way to find out who pushed keepCount to 3? I would suggest that an instance of an object should give a pointer of 1, then I assume that maybe pushing it on the UINavigationController stack can pick it up (not sure about this?), But the third ... is a mystery.

+5
source share
4 answers

Never count directly on accounting. It so happened that during the initialization process, part of the code has a retain ed and autorelease d object. Since you cannot tell how many times the object was autorelease d, you really don't know what the real amount of hold is.

Stored accounts should only be used as debugging aids, and not as a software control flow.

If you follow all the rules outlined in Cocoa's Memory Management Programming Guide , you won't have a problem.

+20
source

Adam is right that you should not worry too much about keeping the score.

But if you ever have a legitimate need to solve such a mystery, a good technique is to subclass the affected class so that you can add overrides to memory management methods.

eg. in a subclass of UIViewController you can implement:

 - (id) retain { // Break here to see who is retaining me. return [super retain]; } 
+43
source

What is the best way to find out who pushed keepCount to 3?

This approaches the problem from the wrong angle. This will confuse you and lead you astray (and probably the direct past), the actual problem when it really is.

It is better to think about who owns the object. Do you intend to save the object as the value of one of your own properties? If so, then you are one of its owners. If not, then this is not so. If you transfer an object to another object for storage in one of its properties, then this other object is also the owner.

These owners are just relationships, so it’s very easy to keep them right in your head.

  • "This is one of my controllers. It owns the root objects of my model and one or more [controller] s views."
  • "This is a view. He owns some parts of my model."
  • "This is part of my model. It owns only primitive objects."
  • "This is another part of my model. It owns some primitive objects and some other bits of the model."

If you have a complete understanding of your owners, you cannot write a memory leak, except that you forget the release or autorelease (which can happen to anyone), and you almost certainly will not write circular save (two objects save each other ), with the exception of knowingly and with plentiful comments and #warnings.

If you did not develop your owners, then you probably wrote one or more memory leaks or cyclic holds that you are not aware of.


Edit: And in order to answer the actual question, the best way to find out what is preserved and, possibly, subsequently auto-implement, is to use the Tool Selection Tools tool . With it, you can view the history of any object to see each distribution, save, auto-detect, release and release its address.

+8
source

This is not a 100% solution, but the LLVM Clang Static Analyzer can be of great help in tracking down the misuse of manual memory management. Between the static analyzer and MallocDebug, you can quickly track memory management issues. By the way, even though the tools are a new heat, I find MallocDebug much more reliable.

Here you can find the LLVM Clang Static Analyzer: LLVM / Clang Static Analyzer

+1
source

All Articles