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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 08-25-2010, 04:11 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default Memory leaks..please help

Dear Developers, I am getting a potential leak in these methods..can anyone please help me..Leak in method 1 is in line 5..I am referencing this method from another class as [self loginHandler]..

Code:
-(void) loginHandler
{
1   SCRMsugarsoap* service = [[SCRMsugarsoap alloc] initWithUrl:serverURL];
2   service.logging = YES;
3   service.username = userName;
4   service.password = password;
5   [service login:self action:@selector(sessionIdHandler:) user_auth: [[[SCRMuser_auth alloc] initWithUsername:userName andPassword:password]autorelease] application_name: @""];
6   [service release];
}
And another method where I have problems with leaks is

Code:
-(NSMutableArray *)searchContacts:(NSString *)tableName bySearchString:(NSString *)searchString
{

1   NSString *sid=@"";
2   NSString *firstname=@"";
3   NSString *lastname=@"";
4   NSString *qsql;
    //NSArray *contactArray=[[NSArray alloc]init];
5   searchArray=[[NSMutableArray alloc]init];

6   qsql=[NSString stringWithFormat:@"SELECT DISTINCT sugar_id,first_name,last_name FROM CONTACTS where last_name LIKE '%%%@%%' OR first_name LIKE '%%%@%%' GROUP BY sugar_id ORDER BY last_name",searchString,searchString];
7   sqlite3_stmt *statement;
8   if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
9       while (sqlite3_step(statement) == SQLITE_ROW) 
10      {
                //TODO: alloc Contact object
11          Contact *contacts=[[Contact alloc]init];

12          sid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
13          firstname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
14          lastname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
15

16          if ([firstname isEqualToString:@"(null)"]) {
                lastname=[lastname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[lastname substringToIndex:1] uppercaseString]];
17              contacts.lastName=lastname;
18              contacts.sugarId=sid;
19              contacts.firstName=@"";


                }
20          else {

21              firstname=[firstname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[firstname substringToIndex:1] uppercaseString]];
22              lastname=[lastname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[lastname substringToIndex:1] uppercaseString]];
23              contacts.firstName=firstname;
24              contacts.lastName=lastname;
25              contacts.sugarId=sid;


            }
26          [searchArray addObject:contacts];
            //searchArray=[NSMutableArray arrayWithObjects:contacts];           
            //TODO:  Release contacts variable
27          [contacts release];
28          [sid release]; //not sure about releasing these objects..just gave a try
29          [firstname release];
30          [lastname release];
31          firstname = nil;
32          lastname = nil;
33          sid=nil;


        }       
34      sqlite3_reset(statement);


    }

35  sqlite3_finalize(statement);
36  return searchArray; 
}
I am releasing searchArray in dealloc method. The leaks are between lines 5,11-14,16,21,22.These lines vary whenever I try to fiddle with the code..please help me guys..waiting for your suggestions..

I tried using instruments and these are the areas figured out using it..Can I also use build and analyze in xcode..I used that and made some changes to the code..
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 04:39 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I'm not seeing it in the first part. If there is one there, it is likely in your custom initializer method.

In the second one, if that method is called multiple times, then you will leak searchArray each time. You should use properties.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-25-2010, 04:47 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

This is the initalizer method for the first method:Because of the leaks in these methods I am unable to install this on my iphone..For the second method I am returning an array but I am accessing the values of the array by calling the method only once..that's the reason I am not making this as a property..please help..thanks for the reply

Code:
- (id) initWithUsername: (NSString*) username andPassword: (NSString*) pass 
	{
		if(self = [super init])
		{
			Soap *converter = [[Soap alloc] init];
			SCRMsugarsoap *service = [[SCRMsugarsoap alloc] init];
			
			[service get_server_version:self action:@selector(get_server_versionHandler:)];
		
			self.user_name = username;
			self.password = [converter tomd5:pass];
			
			[converter release];
			[service release];
		}
		return self;
	}
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 04:50 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Nothing jumping out at me.

If it doesn't need to be a property, then it doesn't need to be an instance variable either. Just use a local variable.

What do you mean you can't install because of leaks? That's very incorrect.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-25-2010, 04:52 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

Quote:
Originally Posted by racharambola5 View Post
This is the initalizer method for the first method:Because of the leaks in these methods I am unable to install this on my iphone..For the second method I am returning an array but I am accessing the values of the array by calling the method only once..that's the reason I am not making this as a property..please help..thanks for the reply

Code:
- (id) initWithUsername: (NSString*) username andPassword: (NSString*) pass 
	{
		if(self = [super init])
		{
			Soap *converter = [[Soap alloc] init];
			SCRMsugarsoap *service = [[SCRMsugarsoap alloc] init];
			
			[service get_server_version:self action:@selector(get_server_versionHandler:)];
		
			self.user_name = username;
			self.password = [converter tomd5:pass];
			
			[converter release];
			[service release];
		}
		return self;
	}
Method for tomd5 is:

Code:
-(NSString*)tomd5:(NSString*)value{
	const char *cStr = [value UTF8String];
	unsigned char result[CC_MD5_DIGEST_LENGTH];
	CC_MD5(cStr, strlen(cStr), result);
	return [[NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
	]autorelease];
}
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 04:53 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
-(NSString*)tomd5:(NSString*)value{
	const char *cStr = [value UTF8String];
	unsigned char result[CC_MD5_DIGEST_LENGTH];
	CC_MD5(cStr, strlen(cStr), result);
	return [[NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
	]autorelease];
}
You don't want the autorelease.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-25-2010, 04:54 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

I can't find the reason why I can't run my app in iphone..I will make it local variable and give a try..if it is a local variable can I release the array once I add the object to it?? I am a newbie just started writing my first app..I am sorry if it sounds incorrect..thanks for your time..
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 04:57 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

Ok I will try with the changes you suggested and will let you know..thankq..
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 04:58 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

The quality of your code has nothing to do with whether or not you can install it. Getting it to run is a different matter.

Leaks do not cause crashes. Leaks can lead to too much memory usage, which eventually will cause the OS to kill your app, but that rarely happens instantly. So if you are crashing quickly, it is highly unlikely to be due to leaks.

Accessing a dead object will crash. There you are looking for something that has been released too much, or something that has not been retained enough.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-25-2010, 05:03 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

Brian,

I took off the autorelease from tomd5 and the leak continues..so it points at loginHandler method which inturn points to initWithUserNameandPassword method which refers to tomd5 method..I can't figure out what's wrong with it..

I made changes to the second method but I am getting leaks when I am creating contact object,getting an sid from database,adding an object to array and converting the first character of firstname and lastname to be caplitalized..I didn't released searchArray..Thank you for your time..your suggestions are helping me to learn..
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 05:08 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

If searchArray is now a local variable, then you would autorelease it on return:

Code:
return [searchArray autorelease];
I can't help with the SQL stuff. I don't know what the rules are.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-25-2010, 05:10 PM   #12 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

It is throwing me this error and the app gets crashed when I tried to include autorelease for the array:

*** -[__NSArrayM objectAtIndex:]: message sent to deallocated instance 0xdf1d8d0



Code:
-(NSMutableArray *)searchContacts:(NSString *)tableName bySearchString:(NSString *)searchString
{
	
	NSString *sid=@"";
	NSString *firstname=@"";
	NSString *lastname=@"";
	NSString *qsql;
	//NSArray *contactArray=[[NSArray alloc]init];
	NSMutableArray *searchArray=[[NSMutableArray alloc]init];
	
	qsql=[NSString stringWithFormat:@"SELECT DISTINCT sugar_id,first_name,last_name FROM CONTACTS where last_name LIKE '%%%@%%' OR first_name LIKE '%%%@%%' GROUP BY sugar_id ORDER BY last_name",searchString,searchString];
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) 
		{
				//TODO: alloc Contact object
			Contact *contacts=[[Contact alloc]init];
		
			sid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
			firstname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
			lastname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];

				
			if ([firstname isEqualToString:@"(null)"]) {
				lastname=[lastname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[lastname substringToIndex:1] uppercaseString]];
				contacts.lastName=lastname;
				contacts.sugarId=sid;
				contacts.firstName=@"";
				

				}
			else {
				
				firstname=[firstname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[firstname substringToIndex:1] uppercaseString]];
				lastname=[lastname stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[lastname substringToIndex:1] uppercaseString]];
				contacts.firstName=firstname;
				contacts.lastName=lastname;
				contacts.sugarId=sid;
			  

			}
			[searchArray addObject:contacts];
			//searchArray=[NSMutableArray arrayWithObjects:contacts];			
			//TODO:  Release contacts variable
			[contacts release];
			//[sid release];
			firstname = nil;
			lastname = nil;
			sid=nil;


		}		
		sqlite3_reset(statement);
			
		
	}

	sqlite3_finalize(statement);

	return [searchArray autorelease];
}
racharambola5 is offline   Reply With Quote
Old 08-25-2010, 06:08 PM   #13 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

It is now correct. You have some other problem. I suggest you read up on the memory management rules and track down your issue.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, memory leaks, memory management, objective-c

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: 347
7 members and 340 guests
dre, freewind, hain, HemiMG, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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