Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2010, 04:06 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 5
Default initWithCoder Memory Leak

The Leaks instrument tells me that I have a memory leak when I use initWithCoder. For example, here is the pattern of what I am doing (somewhat pseudo):

Class.h
{
MyObject *myObject;
}

@property (nonatomic, retain) MyObject *myObject;

Class.m

@synthesize myObject

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

-(id)initWithCoderNSCoder *)decoder{
if (self = [super init]{
self.myObject = [decoder decodeObjectForKey:@"MyObject"];
}
return self;
}

Leaks reports a leak of type NSCFString on the line;
self.myObject = [decoder decodeObjectForKey:@"MyObject];


As I understand it, initWithCoder returns an autoreleased object. Since I immediately assign that value to the myObject property, which is specified as (nontoxic, retain) in the property definition, I retain the autoreleased object through the setter method of the myObject property. The myObject is then released in the dealloc method. I don't understand where the leak is if I understand the sequence correctly. Also why is it reported as a NSCFString when the type is MYObject?

Any thoughts would be appreciated.
DrCronus is offline   Reply With Quote
Old 09-11-2010, 08:37 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Do you change the value of myObject anywhere else? All it would take would be one myObject = blah (without the self.) to leak the loaded object.

Does anyone else grab or retain the object pointed to by myObject? If someone else does a [you.myObject retain] or adds it to an array and leaks the array then your object will never be destroyed.

The leaks tool lets you drill down to the list of retains and releases for each leaked object, so you should be able to examine the life of that object and see who retained it without releasing.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 260
14 members and 246 guests
2WeeksToGo, AdamL, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, prchn4christ, smithdale87, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,747
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:51 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0