Releasing view after added to subview - Memory Mgmt


#1

Regarding the retain count trivia on pg 94.

Since the view is retained by the window when the view is added to its subview, you can release the view. In other words, since the addSubview: method retains view, you can release it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	CGRect wholeWindow = [window bounds];
	view = [[HypnosisView alloc] initWithFrame:wholeWindow];

	[view setBackgroundColor:[UIColor clearColor]];
	[window addSubview:view];
	[view release];

	[window makeKeyAndVisible];
	
	return YES;
}

Thus the alloc is balanced out by the release, all is good and you don’t need to release view in the dealloc method. No?


#2

Correct, you could release it here and everything would be okay.

In practice, I would usually just release as you are suggesting, since the object graph is pretty straightforward.

However, it is safer to do it the way in the book because you keep a pointer to this object as an instance variable, and there really should be a retain for each pointer (the window’s and the controller’s pointers).


#3

[quote=“JoeConway”]Correct, you could release it here and everything would be okay.

In practice, I would usually just release as you are suggesting, since the object graph is pretty straightforward.

However, it is safer to do it the way in the book because you keep a pointer to this object as an instance variable, and there really should be a retain for each pointer (the window’s and the controller’s pointers).[/quote]

You bring up a good point, the “view” shouldn’t be pointing to outer memory space.

Perhaps a better, non-memory leakish way is to provide setters and getters for the view ivar and modifying the code as such…

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
   CGRect wholeWindow = [window bounds];

   HypnosisView *aView = [[HypnosisView alloc] initWithFrame:wholeWindow];
   [self setView:aView];
   [aView release];

   [view setBackgroundColor:[UIColor clearColor]];
   [window addSubview:view];

   [window makeKeyAndVisible];
   
   return YES;
}

#4

Sure, this is fine. But keep in mind a few things:

  1. In your version, the net effect is the same, but there are additional messages being sent.

  2. Exposing it as a property then allows other objects to modify the view, which may not be what you want.

  3. You’ll have to type more to do it this way :slight_smile:


#5

[quote=“JoeConway”]Sure, this is fine. But keep in mind a few things:

  1. In your version, the net effect is the same, but there are additional messages being sent.

  2. Exposing it as a property then allows other objects to modify the view, which may not be what you want.

  3. You’ll have to type more to do it this way :slight_smile:[/quote]

Ok, I think I got it. I was uncomfortable with doing an alloc without the release. BUT, we are releasing the view in the dealloc method. So we are cool. So you are right, my way is no good.

This also pertains to my final comment in the thread: Using UIScrollView
viewtopic.php?f=48&t=293