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 03-15-2010, 05:32 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
Default Memory Leak in AdressBook: What works in Simulator crashes on Device

Hi Folks,

in my app I am reading the contacts of the adressbook into a NSMutableArray:

Code:
ABAddressBookRef addressBook = ABAddressBookCreate();
	
	CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
	CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
	
	masterList = [[NSMutableArray alloc] init];
	userID = [[NSMutableArray alloc] init];
	
	for (int i = 0; i < nPeople; i++) {
		ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
		CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
		CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
		NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(ref)];
		addId = [NSString stringWithFormat:@"%d", (NSNumber *)recordId];
		NSString *contactFirstLast = [NSString stringWithFormat: @"%@, %@",(NSString *)lastName, (NSString *)firstName];
		NSString *userId = [NSString stringWithFormat:@"%@, %@, %@", (NSString *)lastName, (NSString *)firstName, (NSNumber *)recordId];
		CFRelease(firstName);
		CFRelease(lastName);
		
		

		
		
		[masterList addObject:contactFirstLast];
		[userID addObject:userId];
		
		NSLog(@"masterlist: %@", masterList);
		NSLog(@"ID = %d", userID);
	//	[contactFirstLast release];
	}
This is perfectly working in the simulator, but when I am installing the app on my testphone it is crashing...

target remote-mobile /tmp/.XcodeGDBRemote-3659-49
Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
run
Running…
[Switching to thread 11779]
[Switching to thread 11779]
(gdb) continue
Program received signal: “EXC_BAD_ACCESS”.
kill
quit

When I comment out the code above everything is working fine! Do I have a memory leak? Do I have to Release the Number (NSNumber *)recordId?

Thanks for any help!

Stefan
StefanL is offline   Reply With Quote
Old 03-15-2010, 05:55 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

You shouldn't be releasing "contactFirstLast". You don't own the object.
Take a look at the memory management docs if you haven't already.
harrytheshark is offline   Reply With Quote
Old 03-15-2010, 06:32 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 916
Default

I ran into some issues like this too, I think you need to check that firstName and lastName exist before you try to use them, or any other field that you pull from the address. On the simulator the address book has all perfect data where every field is filled in, but on your device you might have entries that don't have a last name or something like that.

So something like this: (This is just an example, because this will ignore any entries that do not have both a first and last name)

Code:
ABAddressBookRef addressBook = ABAddressBookCreate();
	
	CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
	CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
	
	masterList = [[NSMutableArray alloc] init];
	userID = [[NSMutableArray alloc] init];
	
	for (int i = 0; i < nPeople; i++) {
		ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
		CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
		CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
		NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(ref)];
		addId = [NSString stringWithFormat:@"%d", (NSNumber *)recordId];

//New Code
if ((firstName) && (lastName)) {
NSString *contactFirstLast = [NSString stringWithFormat: @"%@, %@",(NSString *)lastName, (NSString *)firstName];
NSString *userId = [NSString stringWithFormat:@"%@, %@, %@", (NSString *)lastName, (NSString *)firstName, (NSNumber *)recordId];
}
		CFRelease(firstName);
		CFRelease(lastName);
		
		

		
		
		[masterList addObject:contactFirstLast];
		[userID addObject:userId];
		
		NSLog(@"masterlist: %@", masterList);
		NSLog(@"ID = %d", userID);
	//	[contactFirstLast release];
	}
lukeca is offline   Reply With Quote
Old 03-15-2010, 08:08 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
Default

Hi Folks,

thanks for your help and assistance!

@harry: I have not releasing the [contactfirstlast] - I have commented it out :-)

@lukeca: You were completely right! I have adapted the code like the following

Code:
	ABAddressBookRef addressBook = ABAddressBookCreate();
	
	if (!addressBook)   
	{  
		NSLog(@"opening address book");  
	}  
	
	CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
	CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
	
	for (int i = 0; i < nPeople; i++) {
		ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
		CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
		CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
		NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(ref)];
		addId = [NSString stringWithFormat:@"%d", (NSNumber *)recordId];
		//New Code
		if ((lastName) && (firstName)) {
			contactFirstLast = [NSString stringWithFormat: @"%@, %@",(NSString *)lastName, (NSString *)firstName];
			userId = [NSString stringWithFormat:@"%@, %@, %@", (NSString *)lastName, (NSString *)firstName, (NSNumber *)recordId];
		} else {
		
			NSLog(@"bingo");
		
		}
		CFRelease(firstName);
		CFRelease(lastName);
		CFRelease(ref); 
		
		

		
		
		[masterList addObject:contactFirstLast];
		[userID addObject:userId];
		
		NSLog(@"masterlist: %@", masterList);
		NSLog(@"ID = %@", userID);
	}
But I am not sure what can I do to show the Names even if I have only the first or the lastname (at least one of them should be in the addressbook)... Thanks for your help!

BR,

Stefan
StefanL is offline   Reply With Quote
Reply

Bookmarks

Tags
adressbook, kabpersonlastnameproperty, memory

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: 275
20 members and 255 guests
ADY, Bertrand21, Dani77, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, jimbo, macquitzon216, MACralik, mer10, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84, Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
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 03:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0