08-26-2010, 10:45 AM
#1 (permalink )
Registered Member
Join Date: Aug 2010
Posts: 60
Memory allocations and Leaks on iphone..Please help..
Hey All,
I have problem with the way I treat the memory management in iphone..please please help me..I am running out of ideas..I tried to play with the below method but it started leaks when I am trying to allocate the strings for the objects (for example allocation for sid,first_name etc)..I can't figure out how to do..and this is the method where I will get all the contacts from the service and insert it into the database..It consumes more memory which I figured out by running allocations instrument on iphone and my app on iphone crashes..please please help me guys..I think my app consumes more memory but not sure where to look into it..I can post any code if this is not clear..thanks a lot!!!
Code:
- (void) get_entry_listHandler: (id) value{
NSString *salutation,*sugar_id,*first_name,*last_name,*title1,*department,*phone_home,*phone_mobile,*phone_work,*phone_other,*phone_fax,*assistant,*assistant_phone,*primary_address_street,*primary_address_city,*primary_address_state,*primary_address_postalcode,*custom;
NSString *primary_address_country,*alt_address_street,*alt_address_city,*alt_address_state,*alt_address_postalcode,*alt_address_country,*date_entered,*date_modified,*deleted,*do_not_call,*birthdate,*lead_source,*description,*email1,*email2,*account_name,*account_id,*id1;
SCRMget_entry_list_result* result = (SCRMget_entry_list_result*)value;
SCRMsugarsoap* service = [[SCRMsugarsoap alloc] initWithUrl:serverURL];
SCRMentry_value* entry;
for(NSString * myStr in result.entry_list) {
entry = (SCRMentry_value*)myStr;
for(NSString * myStr1 in entry.name_value_list) {
SCRMname_value* value1 = (SCRMname_value*)myStr1;
if ([value1.name isEqualToString:@"id"])
sugar_id = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"salutation"])
salutation = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"first_name"])
first_name = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"last_name"])
last_name = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"title"])
title1 = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"department"])
department = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"phone_home"])
phone_home = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"phone_mobile"])
phone_mobile = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"phone_work"])
phone_work = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"phone_other"])
phone_other = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"phone_fax"])
phone_fax = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"assistant"])
assistant = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"assistant_phone"])
assistant_phone = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"primary_address_street"])
primary_address_street = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"primary_address_city"])
primary_address_city = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"primary_address_state"])
primary_address_state = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"primary_address_postalcode"])
primary_address_postalcode = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"primary_address_country"])
primary_address_country = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"alt_address_street"])
alt_address_street = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"alt_address_city"])
alt_address_city = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"alt_address_state"])
alt_address_state = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"alt_address_postalcode"])
alt_address_postalcode = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"alt_address_country"])
alt_address_country = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"date_entered"])
date_entered = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"date_modified"])
date_modified = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"deleted"])
deleted = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"do_not_call"])
do_not_call = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"birthdate"])
birthdate = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"lead_source"])
lead_source = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"description"])
description = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"email1"])
email1 = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"email2"])
email2 = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"account_name"])
account_name = [[NSString alloc] initWithFormat: @"%@",value1.value];
else if ([value1.name isEqualToString:@"account_id"])
account_id = [[NSString alloc] initWithFormat: @"%@",value1.value];
else
custom = [[NSString alloc] initWithFormat: @"%@",value1.value];
}
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSDate *date_moded = [df dateFromString:date_modified];
NSDate *lastSync = [df dateFromString:lastSyncTime];
NSDate *date_enter = [df dateFromString:date_entered];
[df release];
if ([[date_moded laterDate:lastSync]isEqualToDate:date_moded] && lastSyncTime != nil && [date_enter isEqualToDate:date_moded])
id1 = [[NSString alloc]initWithFormat:@"%d",primaryKeyCount];
else if ([[date_moded laterDate:lastSync]isEqualToDate:date_moded] && lastSyncTime != nil )
id1 = [db getPrimaryKey:sugar_id];
else
id1 = [[NSString alloc]initWithFormat:@"%d",primaryKeyCount];
[db insertRecordIntoTableNamed: @"Contacts" withField1: @"id" field1Value: id1 andField2: @"sugar_id" field2Value: sugar_id
andField3: @"salutation" field3Value: salutation andField4: @"first_name" field4Value: first_name
andField5: @"last_name" field5Value: last_name andField6: @"title" field6Value: title1
andField7: @"department" field7Value: department andField8: @"phone_home" field8Value: phone_home
andField9: @"phone_mobile" field9Value: phone_mobile andField10: @"phone_work" field10Value: phone_work
andField11: @"phone_other" field11Value: phone_other andField12: @"phone_fax" field12Value: phone_fax
andField13: @"assistant" field13Value: assistant andField14: @"assistant_phone" field14Value: assistant_phone
andField15: @"primary_address_street" field15Value: primary_address_street andField16: @"primary_address_city" field16Value: primary_address_city
andField17: @"primary_address_state" field17Value: primary_address_state andField18: @"primary_address_postalcode" field18Value: primary_address_postalcode
andField19: @"primary_address_country" field19Value: primary_address_country andField20: @"alt_address_street" field20Value: alt_address_street
andField21: @"alt_address_city" field21Value: alt_address_city andField22: @"alt_address_state" field22Value: alt_address_state
08-26-2010, 10:45 AM
#2 (permalink )
Registered Member
Join Date: Aug 2010
Posts: 60
Code:
andField23: @"alt_address_postalcode" field23Value: alt_address_postalcode andField24: @"alt_address_country" field24Value: alt_address_country
andField25: @"date_entered" field25Value: date_entered andField26: @"date_modified" field26Value: date_modified
andField27: @"deleted" field27Value: deleted andField28: @"do_not_call" field28Value: do_not_call
andField29: @"birthdate" field29Value: birthdate andField30: @"lead_source" field30Value: lead_source
andField31: @"description" field31Value: description andField32: @"email1" field32Value: email1
andField33: @"email2" field33Value: email2 andField34: @"account_name" field34Value: account_name
andField35: @"account_id" field35Value: account_id andField36: @"custom" field36Value: custom];
primaryKeyCount++;
currentCount++;
}
[salutation release];
[sugar_id release];
[first_name release];
[last_name release];
[title1 release];
[department release];
[phone_home release];
[phone_mobile release];
[phone_work release];
[phone_fax release];
[phone_other release];
[assistant release];
[assistant_phone release];
[primary_address_street release];
[primary_address_city release];
[primary_address_state release];
[primary_address_postalcode release];
[primary_address_country release];
[alt_address_street release];
[alt_address_city release];
[alt_address_state release];
[alt_address_postalcode release];
[alt_address_country release];
[custom release];
[date_entered release];
[deleted release];
[do_not_call release];
[birthdate release];
[lead_source release];
[description release];
[email1 release];
[email2 release];
[account_name release];
[account_id release];
float expected = (float) contactEntries;
float gotSoFar = (float) currentCount;
syncViewController.progressView.progress = ((float)((gotSoFar - 1) / (expected + 1)));
syncViewController.progressLabel.text=[NSString stringWithFormat:@"Syncing %d Contacts",contactEntries];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
integer = [NSNumber numberWithInt:primaryKeyCount];
[defaults setObject:integer forKey:@"primaryKeyCount"];
[defaults synchronize];
moduleViewController.db = db;
int a = result.next_offset;
int b = contactEntries;
while (result.result_count == 20 /*&& result.next_offset < contactEntries*/) {
if ( lastSyncTime == nil) {
[service get_entry_list:self action:@selector(get_entry_listHandler:) session:sessionId module_name: @"Contacts" query: @"" order_by: @"" offset: result.next_offset select_fields: [[[SCRMselect_fields alloc] init]autorelease] max_results: contactEntries deleted: 0];
[service release];
return;
//break;
}
else {
[service get_entry_list:self action:@selector(get_entry_listHandler:) session:sessionId module_name: @"Contacts" query: finalQuery order_by: @"" offset: result.next_offset select_fields: [[[SCRMselect_fields alloc] init]autorelease] max_results: contactEntries deleted: 0];
[service release];
return;
//break;
}
}
if (a >= b ) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[settingsViewController.navigationController pushViewController:moduleViewController animated:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:@"reset"];
[defaults synchronize];
[finalQuery release];
}
[service release];
[id1 release];
}
This memory goes all the way upto 700MB as you see in the figure but comes down when it fetches all the contacts..I tried in all the ways..thanks for your time..
08-26-2010, 11:26 AM
#3 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
Every time you use [NSString alloc], you should do it this way:
Code:
{
NSString *strTemp = [[NSString alloc] initWithFormat: @"%@",value1.value];
self.sugar_id = strTemp;
[strTemp release];
}
which means, of course, that you have to make sugar_id and all those other similar NSString* local variables into properties declared with the "retain" keyword. This way the minimum amount of memory is consumed.
08-26-2010, 11:58 AM
#4 (permalink )
Registered Member
Join Date: Aug 2010
Posts: 60
Scott,
Thanks for the reply...I will give it a try and will let you know..
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38