0

I am check the retainCount for the subview after add to the view. the code is :

- (void) loadView{
    //...
    toolbar = [[UIToolbar alloc] initWithFrame:nil];
    [[self view] addSubView:toolbar];
}

- (void) dealloc{
    NSLog(@"count=%d", [toolbar retainCount]);   // count=2
    [toolbar removeFromSuperView];
    NSLog(@"count=%d", [toolbar retainCount]);   // count=1
    [toolbar release]
    NSLog(@"count=%d", [toolbar retainCount]);   // count=1
    toolbar = nil;
    NSLog(@"count=%d", [toolbar retainCount]);   // count=0
}

From this code in the dealloc{}, I have some questions:

1, the first log retainCount for toolbar is 2, because the toolbar after init and added to the self view, the retaincount will become to 2.

2, After the toolbar removeFromSuperView, the retainCount will become to 1.

3, After the toolbar call the release method, the retainCount is still 1, it can not become 0. In my options, because the super view is remain the toolbar(the self view is not release), so the toolbar can not reduse to 0.

4, If call the toolbar set to nil ,the retainCount will be 0. This log is useless.

My questions is:

a)the toolbar call the removeFromSuperView and the release API, the result is the same, the toolbar retainCount will only become to 1. So If i test the code only use each of them, the result is same. So, can I conclude user can only call each one API is OK?

b)From the apple document, the subview is added to the self view, the self view has remain the toolbar handle, so in the dealloc method, if the viewDidUnload is not called, the toolbar retainCount can not be reduce to 0. If the memory is low, the system will reduce the unuseless view and call the viewDidUnload method, it will automatic reduce the toolbar retainCount to 0. So in the dealloc method, should I set the toolbar to nil. I am confuse if I set the toolbar to nil, when the viewDidUnload method is called, the toolbar will be reduced or not? Is there any momory leak?

Thx.

8
  • But for the subview memory, when the subview will be release release, it should call only the memory is low and in the viewDidUnload method?
    – Golden
    Jun 14, 2012 at 7:38
  • Test in instruments for memory leaks. Checking retain counts like this will tell you nothing. Jun 14, 2012 at 7:41
  • 1
    You should never use retainCount: stackoverflow.com/questions/11028512/…
    – rckoenes
    Jun 14, 2012 at 7:43
  • I have some memory issue, use the Instruments Allocations for check the momory, the momery will slow increase.
    – Golden
    Jun 14, 2012 at 7:44
  • could you provide the declaration for your toolbar property? Is it retain/strong or weak/assign? are you using ARC?
    – sergio
    Jun 14, 2012 at 7:50

1 Answer 1

2

First off, if your code reflects your real code, your dealloc method has a big issue:

- (void) dealloc{
   [toolbar removeFromSuperView];
   [toolbar release]
   toolbar = nil;
}

you are not calling [super dealloc]. By not calling [super dealloc], self.view will never be released (and eventually deallocated).

This should fix your memory leaks (partially, at least):

- (void) dealloc{
    [toolbar release];
    [super dealloc];
}

You can notice that I have removed the call to removeFromSuperView because this is automatically done for you when self.view is actually deallocated, so you don't need to do it yourself. In any case, calling removeFromSuperView does not cause any problem either.

About your questions, I assume that your toolbar property is declared as retain (based on the code you submit, it is the most sensed hypothesis to me).

If toolbar is a retain kid of property, then the correct way to assign to it a newly created view is:

 toolbar = [[[UIToolbar alloc] initWithFrame:nil] autorelease];

please, note the autorelease; if it is not there your retain/release calls are impaired. that could explain the need for you to first call release then nil the property in dealloc:

- (void)dealloc {
 ...
    [toolbar release]
    toolbar = nil;
 ....
}

by doing like that you are releasing toolbar twice; but since you did not use autorelease when assigning to the retain property, this will produce the correct result.

a)the toolbar call the removeFromSuperView and the release API, the result is the same, the toolbar retainCount will only become to 1. So If i test the code only use each of them, the result is same. So, can I conclude user can only call each one API is OK?

as I said, you don't need to call directly removeFromSuperView to get a subview to be released at dealloc time, since self.view will do that for you. It is a different matter when you want to remove a subview while still keeping the superview around (imagine you display a label and then remove it); in this case you need to call both, otherwise you have a leak.

b)From the apple document, the subview is added to the self view, the self view has remain the toolbar handle, so in the dealloc method, if the viewDidUnload is not called, the toolbar retainCount can not be reduce to 0. If the memory is low, the system will reduce the unuseless view and call the viewDidUnload method, it will automatic reduce the toolbar retainCount to 0. So in the dealloc method, should I set the toolbar to nil. I am confuse if I set the toolbar to nil, when the viewDidUnload method is called, the toolbar will be reduced or not? Is there any momory leak?

if I understand your doubt correctly, the point is that if you sent the toolbar property to nil in viewDidUnload, then when this method is called (either when you explicitly delete the view or when a memory warning is issued) your subview is correctly disposed of; if dealloc for your controller is called henceforth, your property has already a nil value, so releasing it will not have any effect (but that is fine, since it was already released in viewDidUnload).

On the other hand, if you do not release your property in viewDidUnload then what happens is that in case the view is displayed again after a memory warning, then loadView/viewDidLoad is called again; but in this case, when you create the toolbar subview and assign its reference to the toolbar property (assuming it is retain kind), then the older object is automatically released for you, so you don't have any memory leak; what happens is that you are using a bit more memory that you could possibly do (since the toolbar is not deallocated until when self.view is created again).

2
  • sorry, i fogot to line the super.
    – Golden
    Jun 14, 2012 at 7:57
  • 2
    You forgot: "retainCount is useless. Don't call it."
    – bbum
    Jun 14, 2012 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.