On 7 Feb 2009, at 8:13 am, James Cicenia wrote:

Hello -

I have the example code:

Can I do this.. return a view and it will stick around?

Thanks
James Cicenia


-(UIView *)mapViewForMonthAndType:(NSString *) pMonth type: (NSString) pType{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        
        NSEnumerator *itemEnum = [availabilityArray objectEnumerator];
        AvailableItem *item = nil;
        
UIImageView *stateView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 220)];
        [stateView setImage: [UIImage imageNamed:@"wf-map-back.png"]];

        return stateView;
}


By "the rules" this will leak 'stateView'.

Because this method name doesn't include 'new', 'copy', 'mutableCopy, or 'alloc' it means that the object returned is not owned by the caller. So it should be returning an autoreleased object. Because it's not autoreleased, it's leaking.

If the caller of this method "knows" that this is the case, and assumes ownership of the object, you've broken the rules (two wrongs don't make a right). The client would then be making an assumption about the implementation details that go beyond the common rules. Any code that uses this method would then also have to similarly break the rules to avoid the leak.

Review:

http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

and in particular:

http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html#/ /apple_ref/doc/uid/20000994

--Graham




_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to