Under ARC, compiler will forbid using any method or selector of -retainCount
, -retain
, -dealloc
, -release
, and -autorelease
.
But sometimes I want to know the retain counts at runtime, or using method swizzling to swap the -dealloc
method of NSObject to do something.
Is it possible to suppress (or bypass) the compiler complaining just a couple of lines of code? I don't want to modify ARC environment for whole project or whole file. I think preprocessor can do it, but how?
Additions:
Thanks guys to give me a lesson about the use of -retainCount
. But I wonder whether it is possible to enforce invoking/using those forbidden methods/selectors.
I know Instruments
is a powerful tool to do this job. But I am still curious about those question.
Why do I want to use -retainCount
:
When using block, if you don't specify a __weak
identifier on the outside variables, the block will automatically retain those outside objects in the block after the block is copied into the heap. So you need to use a weak self to avoid retain cycle, for example:
__weak typeof(self) weakSelf = self;
self.completionBlock = ^{
[weakSelf doSomething];
};
However, it will still cause retain cycle when you only use instance variables in the copied block (YES, although you didn't use any keyword self
in the block).
For example, under Non-ARC:
// Current self's retain count is 1
NSLog(@"self retainCount: %d", [self retainCount]);
// Create a completion block
CompletionBlock completionBlock = ^{
// Using instance vaiable in the block will strongly retain the `self` object after copying this block into heap.
[_delegate doSomething];
};
// Current self's retain count is still 1
NSLog(@"self retainCount: %d", [self retainCount]);
// This will cuase retain cycle after copying the block.
self.completionBlock = completionBlock;
// Current self's retain count is 2 now.
NSLog(@"self retainCount: %d", [self retainCount]);
Without using -retainCount
before/after the copied block code, I don't think this retain cycle caused by using instance variables in the completion block will be discovered easily.
Why do I want to use -dealloc
:
I want to know whether I can use method swizzling to monitor which object will be deallocated by logging messages on the Xcode console when the -dealloc
is invoked. I want to replace the original implementation of -dealloc
of NSObject
.
Agreed 100% with the other commenters about the fact that you do not want to use
-retainCount
. To your other question, however, about-dealloc
:You also do not want to swizzle
-dealloc
. If you think you want to swizzle it, you don't understand how it works. There are a lot of optimizations going on there; you can't just mess with it. But, as @bbum hints at, you can easily get notifications when objects are deallocated, and this can be very useful.You attach an associated object to the thing you want to watch. When the thing you want to watch goes away, so does the associated object, and you can override its
dealloc
to perform whatever action you want. Obviously you need to be a little careful, because you're in the middle of adealloc
, but you can generally do most anything you'd need to here. Most importantly for many cases, you can put a breakpoint here or add a logging statement, so you can see where the object was released. Here's a simple example.With ARC
Without ARC
Now, when
something
goes away,-[Watcher dealloc]
will fire and log for you. Very easy. Completely supported and documented.EDIT:
You are somewhat correct here, but there are two lessons to be learned, and neither is to use
retainCount
(which won't actually help you in this case anyway becauseretainCount
can very often be something you don't expect).That's not recommened at all, I dont know your intentions but they dont sound very safe.
The use of
retainCount
is not recommended.From AppleDocs:
And, if there's any doubt, check this link:
http://whentouseretaincount.com/
Whatever you are trying to do, please dont.
For future references, I'm going to add some linsk to help you understand the process of how memory works in iOS. Even if you use ARC, this is a must know (remember that ARC is NOT a garbage collector)
Beginning ARC in iOS 5 Tutorial Part 1
Understand memory management under ARC
Memory Management Tutorial for iOS
Advance Memory Managment
And, of course, once you understand how memory works is time to learn how to profile it with instruments:
Instruments User Guide