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 Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 12-21-2009, 06:11 AM   #1 (permalink)
dda
Registered Member
 
Join Date: Nov 2009
Posts: 42
Default Instruments detecting leaks : need help !

Hi,

Instruments detected some leaks in my code but I don't understand where the problem is. To me everything seems to be release correctly but I'm just a beginner in Objective-C :-)

Maybe could someone help me to understand my mistakes here.

In table mode, the detail pane is indicating in the Leaked Object column my object Person (6 times for the length of my Instruments recording with each time the same stack trace)
Note : by the way I'm very astonished to see also a lot of Foundation, ImageIO, UIKit, CoreGraphics objects mentioned as leaked objects in the detail pane.
Can it be that Apple's frameworks are full of leaks !!!!

The extended detail pane is showing a stack indicating that the leak seems to happen when calling -[Person setPartnersWithPrimaryKey:andPreviousPartner:datab ase:]

If I go down in the detail pane to the code, it's showing me :

Code:
- (void)setPartnersWithPrimaryKey:(NSInteger)pk andPreviousPartner:(Person*) thePreviousPartner database:(sqlite3 *)db {
	const char *partnersSql = "SELECT person_id FROM person, relation_list_list, relation, partner_list_list WHERE person.relation_list_id = relation_list_list.relation_list_id AND relation_list_list.relation_id = relation.relation_pk AND relation.partner_list_id = partner_list_list.partner_list_id AND pk = ? AND person_id <> ?";
	
	if (partners_statement != nil) {
		sqlite3_finalize(partners_statement);
		partners_statement = nil;
	}
	
	if (sqlite3_prepare_v2(database, partnersSql, -1, &partners_statement, NULL) != SQLITE_OK)
		NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
	
	sqlite3_bind_int(partners_statement, 1, pk);
	sqlite3_bind_int(partners_statement, 2, pk);

	
	NSMutableArray *aPartnersArray = [[NSMutableArray alloc] init];
		
	while (sqlite3_step(partners_statement) == SQLITE_ROW) {
		int partner_id = sqlite3_column_int(partners_statement, 0);
		Person *aPartner;
		if (partner_id == thePreviousPartner.primaryKey) {
			aPartner = thePreviousPartner;
		} else {
			aPartner = [[Person alloc] initWithPrimaryKey:partner_id database:db];
		}		
		[aPartnersArray addObject:aPartner];
		[aPartner release];		
	}
	self.partners = aPartnersArray;
	[aPartnersArray release];
}
with 3 highlighted lines in this code which are :
1) NSMutableArray *aPartnersArray = [[NSMutableArray alloc] init];
2) aPartner = [[Person alloc] initWithPrimaryKeyartner_id database:db];
3) [aPartnersArray addObject:aPartner];
To me aParnersArray and aPartner are released in this code.
So, where is the problem ? Where is the leak ?

For completeness, here is the code of initWithPrimaryKey

Code:
- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db {
    if (self = [super init]) {
        primaryKey = pk;
        database = db;
		if (init_statement != nil) {
			sqlite3_finalize(init_statement);
			init_statement = nil;
		}	
		// Compile the query for retrieving person data. See insertNewBookIntoDatabase: for more detail.
		// Note the '?' at the end of the query. This is a parameter which can be replaced by a bound variable.
		// This is a great way to optimize because frequently used queries can be compiled once, then with each
		// use new variable values can be bound to placeholders.
		const char *sql = "SELECT firstname,lastname, picture, birthdate, lastdate, father_id, mother_id, sex, me FROM person WHERE pk=?";
		if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
			NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
		}
        // For this query, we bind the primary key to the first (and only) placeholder in the statement.
        // Note that the parameters are numbered from 1, not from 0.
        sqlite3_bind_int(init_statement, 1, primaryKey);
        if (sqlite3_step(init_statement) == SQLITE_ROW) {
            self.firstname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
			self.lastname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 1)];
			double dateTest = sqlite3_column_bytes(init_statement, 3);
			if (dateTest != 0) 				
				self.birthdate = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(init_statement, 3)];
			else {
				self.birthdate = nil;
			}
			dateTest = sqlite3_column_bytes(init_statement, 4);
			if (dateTest != 0) 				
				self.lastdate = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(init_statement, 4)];
			else {
				self.lastdate = nil;
			}
			NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(init_statement, 2) length:sqlite3_column_bytes(init_statement, 2)]; 
			if(data == nil)
				self.picture = nil;
				//[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"default_person_picture.png" ofType:nil]];
			else {
				self.picture = [UIImage imageWithData:data];
				[data release];
			}
			self.father_id = sqlite3_column_int(init_statement, 5);
			self.mother_id = sqlite3_column_int(init_statement, 6);
			self.sex = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 7)];
			if (sqlite3_column_int(init_statement, 8) == 1) self.me = YES;
		
			dirty = NO;
			
			
        }
    }
    return self;
}
Thanks in advance for your help.

Regards,

Didier
dda is offline   Reply With Quote
Old 12-21-2009, 02:39 PM   #2 (permalink)
dda
Registered Member
 
Join Date: Nov 2009
Posts: 42
Default

OK, I found out that my dealloc method was not including some objects that I've added later in my Person Class.
dda is offline   Reply With Quote
Old 12-21-2009, 05:47 PM   #3 (permalink)
dda
Registered Member
 
Join Date: Nov 2009
Posts: 42
Default

I still have leaks appearing in my Person initWithPrimaryKey method.
The leaked objects are in fact NSCFString with Responsible Library : Foundation and Responsible Frame : -[NSPlaceholderString

The stack in the Extreme Detailed Pane shows the +[NSString stringWithUTF8String] called in initWithPrimaryKey to set the firstnames and lastnames of my Person objects.

Is it a know problem that those NSStrings produced by stringWithUTF8String are leaking ?

Regards,

DDA
dda is offline   Reply With Quote
Old 03-03-2010, 09:35 AM   #4 (permalink)
Mobile Application Dev.
 
Join Date: Oct 2008
Location: Bangalore, india
Posts: 362
Default

i am also having the same problem of Placeholder string NSCFString
mpramodjain 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: 251
22 members and 229 guests
@sandris, ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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