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 02-05-2010, 11:39 PM   #1 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default help with memory leaks

This app that I made a while back keeps crashing, it also shows multiple memory leaks in instruments, I can't see where these are coming from because I released all of the objects that I allocated so it doesn't make sense to me that there would be a leak. any help/thoughts would be greatly appreciated. ad btw, I've just started objective-C about a week ago and I've been coding in C++ for over a year, and that's where a lot of my knowledge comes from. thanks again :P

here's the ViewController.mm

Code:
#import "EncryptViewController.h"
#import "table.h"
#import <iostream>
#import <string>
#define MAXLENGTH 300

@implementation EncryptViewController

-(IBAction)encrypt {
	UIAlertView *ealert = [[UIAlertView alloc]initWithTitle:@"Encrypt" message:@"Enter Text to encrypt" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Encrypt", nil];
	[ealert addTextFieldWithValue:nil label:@""];
	[[ealert textField] setDelegate:self];
	[[ealert textField] setKeyboardType:UIKeyboardTypeDefault];
	[[ealert textField] setKeyboardAppearance:UIKeyboardAppearanceDefault];
	ealert.tag = 1;
	[ealert show];
	[ealert release];
	
}

-(IBAction)decrypt {
	UIAlertView *dalert = [[UIAlertView alloc]initWithTitle:@"Decrypt" message:@"Enter Text to decrypt" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Decrypt", nil];
	[dalert addTextFieldWithValue:nil label:@""];
	[[dalert textField] setDelegate:self];
	[[dalert textField] setKeyboardType:UIKeyboardTypeDefault];
	[[dalert textField] setKeyboardAppearance:UIKeyboardAppearanceDefault];
	dalert.tag = 2;
	[dalert show];
	[dalert release];
	
}

using namespace std;


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (alertView.tag == 1) {
		NSString *temp = [[alertView textField] text];
		string phrase = [temp cString];
		int a;
	for (a=0; a<MAXLENGTH; a++) {
		char have;
		int tabe;
		have = phrase[a];
		for (tabe=0; tabe < 26; tabe++)
		{
			if (table[0][tabe] == have){
				phrase[a] = (table[17][tabe]);
				break; }
			else if (table[1][tabe] == have){
				phrase[a] = (table[18][tabe]);
				break; }
			else if (table[2][tabe] == have){
				phrase[a] = (table[19][tabe]);
				break; }
			else
				continue;
		}
		
	}
		const char* tempt = phrase.c_str();
		NSString *ans = [[NSString alloc] initWithCString: tempt];
		UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"Your encrypted message is:" message: ans delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
		[anse show];
		[anse release];
		[ans release];
	}
	else if (alertView.tag == 2) {
		NSString *temp = [[alertView textField] text];
		string phrase = [temp cString];
		int a;
		for (a=0; a<MAXLENGTH; a++) {
			char have;
			int tabe;
			have = phrase[a];
			for (tabe=0; tabe < 26; tabe++)
			{
				if (table[17][tabe] == have){
					phrase[a] = (table[0][tabe]);
					break; }
				else if (table[18][tabe] == have){
					phrase[a] = (table[1][tabe]);
					break; }
				else if (table[19][tabe] == have){
					phrase[a] = (table[2][tabe]);
					break; }
				else
					continue;
			}
		}
		const char* tempt = phrase.c_str();
		NSString *ans = [[NSString alloc] initWithCString: tempt];
		UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"Your decrypted message is:" message: ans delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
		[anse show];
		[anse release];
		[ans release];
	}
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


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

@end
flyingguitar is offline   Reply With Quote
Old 02-06-2010, 12:45 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

addTextFieldWithValue: is a private method, so your app will be rejected for using it. The corresponding text field methods are also off limits.


No leaks are jumping out at me. Are you sure they originate from this code?
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-06-2010, 01:27 AM   #3 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

Quote:
Originally Posted by BrianSlick View Post
addTextFieldWithValue: is a private method, so your app will be rejected for using it. The corresponding text field methods are also off limits.
Yo

No leaks are jumping out at me. Are you sure they originate from this code?
If addTextFieldWithValue: will get rejected, what do you suggest as an alternitive?

And I'm almost positive that the problem origonates from that code, I don't know where else it would be coming from. Also, I'm not even positive it's a memory issue, but somethings causing my app to crash after about a minute. And instruments says I have multiple leaks. But it says I have leaks in almost every app I make (this one in particular), so I'm doing something wrong, hence I came here for help.

But thanks for the fast reply :P
flyingguitar is offline   Reply With Quote
Old 02-06-2010, 02:35 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 13
Default

Quote:
Originally Posted by flyingguitar View Post
If addTextFieldWithValue: will get rejected, what do you suggest as an alternitive?

And I'm almost positive that the problem origonates from that code, I don't know where else it would be coming from. Also, I'm not even positive it's a memory issue, but somethings causing my app to crash after about a minute. And instruments says I have multiple leaks. But it says I have leaks in almost every app I make (this one in particular), so I'm doing something wrong, hence I came here for help.

But thanks for the fast reply :P
What does it say the leaks are from? In building the apps in the Beginning iPhone 3 Development book, I checked with the instrument and there were small leaks already just from the code right out of the box from Apple (before I added any code). I asked the instructor of the class and he said there could be leaks in the Apple-provided stuff. The ones I saw were in core graphics. However, they were minor and likely wouldn't cause a crash.
chucky is offline   Reply With Quote
Old 02-06-2010, 05:45 AM   #5 (permalink)
Registered Member
 
ChrisMayer's Avatar
 
Join Date: Aug 2009
Posts: 238
Default

Quite often Apple's code creates leaks. Where I've found this to happen I've submitted my App to Apple and it's been approved.
__________________
ChrisMayer is offline   Reply With Quote
Old 02-06-2010, 05:48 AM   #6 (permalink)
girls? any new species ?
 
sindhutiwari's Avatar
 
Join Date: Nov 2008
Location: INDIA
Posts: 547
Send a message via MSN to sindhutiwari Send a message via Yahoo to sindhutiwari Send a message via Skype™ to sindhutiwari
Default

@above,

Yes you are correct, even my apps do have memory leaks but yep they are accepted on first submission.

Quote:
Originally Posted by ChrisMayer View Post
Quite often Apple's code creates leaks. Where I've found this to happen I've submitted my App to Apple and it's been approved.
__________________
In order to succeed, your desire for success has to be greater than your fear for failure
sindhutiwari is offline   Reply With Quote
Old 02-06-2010, 10:28 AM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

They don't reject for leaks.

As far as the text fields:
http://www.iphonedevsdk.com/forum/ip...ew-way-do.html
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-06-2010, 11:10 AM   #8 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

Thanks for all of thr support guys, I'll post a screen shot of the leaks I'm getting:



it say's that the leaks are coming from these 2 lines:

From AppDelegate.m
Code:
 [window addSubview:viewController.view];
&

From main.m
Code:
int retVal = UIApplicationMain(argc, argv, nil, nil);

Both of the previous lines of code were generated by apple and can give me up to 30 leaks before it crashes.
flyingguitar is offline   Reply With Quote
Old 02-06-2010, 01:22 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

I generally don't worry about it unless I see my code on the right side.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-06-2010, 04:07 PM   #10 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

but then why is it crashing?

EDIT: nvm, I found that the reason for the crashing was the for loops, I calculated 7,800 loops for each time the button was pressed, that might be a little too much for the iPhone to handle, I'll try to reduce that and see how that effects the performance.

EDIT2: FIXED DD I'll post the new code below to show how I resolved the problem (just in case you cared XD)

and btw, I don't plan on release this on the appstore, so it doesn't matter if I used the private API, but thanks for the tip anyway, I'll try to get into the habit of not using those.

Code:
#import "iEncryptViewController.h"
#import "table.h"
#import <iostream>
#import <string>
#define MAXLENGTH 30

@implementation iEncryptViewController

-(IBAction)encrypt {
	UIAlertView *ealert = [[UIAlertView alloc]initWithTitle:@"Encrypt" message:@"Enter Text to encrypt" delegate:self cancelButtonTitle: nil otherButtonTitles:@"Encrypt", nil];
	[ealert addTextFieldWithValue:nil label:@""];
	[[ealert textField] setDelegate:self];
	[[ealert textField] setKeyboardType:UIKeyboardTypeDefault];
	[[ealert textField] setKeyboardAppearance:UIKeyboardAppearanceDefault];
	[[ealert textField] setReturnKeyType:UIReturnKeyDone];
	[[ealert textField] setAutocorrectionType:UITextAutocorrectionTypeNo];
	ealert.tag = 1;
	[ealert show];
	[ealert release];
	
}

-(IBAction)decrypt {
	UIAlertView *dalert = [[UIAlertView alloc]initWithTitle:@"Decrypt" message:@"Enter Text to decrypt" delegate:self cancelButtonTitle: nil otherButtonTitles:@"Decrypt", nil];
	[dalert addTextFieldWithValue:nil label:@""];
	[[dalert textField] setDelegate:self];
	[[dalert textField] setKeyboardType:UIKeyboardTypeDefault];
	[[dalert textField] setKeyboardAppearance:UIKeyboardAppearanceDefault];
	[[dalert textField] setReturnKeyType:UIReturnKeyDone];
	[[dalert textField] setAutocorrectionType:UITextAutocorrectionTypeNo];
	dalert.tag = 2;
	[dalert show];
	[dalert release];
	
}

using namespace std;


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (alertView.tag == 1) {
		if ((([[alertView textField] text].length) > 0) && (([[alertView textField] text].length) < MAXLENGTH)) {
		NSString *temp = [[alertView textField] text];
		string phrase = [temp cString];
		int a;
		int b = ([[alertView textField] text].length);
	for (a=0; a < b; a++) {
		char have;
		int tabe;
		have = phrase[a];
		for (tabe=0; tabe < 26; tabe++)
		{
			if (table[0][tabe] == have){
				phrase[a] = (table[17][tabe]);
				break; }
			else if (table[1][tabe] == have){
				phrase[a] = (table[18][tabe]);
				break; }
			else if (table[2][tabe] == have){
				phrase[a] = (table[19][tabe]);
				break; }
			else
				continue;
		}
	}
		const char* tempt = phrase.c_str();
		NSString *ans = [[NSString alloc] initWithCString: tempt];
		UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"Your encrypted message is:" message: ans delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
		[anse show];
		[anse release];
		[ans release];
		}
		else if (([[alertView textField] text].length) > MAXLENGTH) {
			UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"ERROR" message: @"The text you have entered is too long to be encrypted, try shortening the message." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
			[anse show];
			[anse release];
		}
	}	
	else if (alertView.tag == 2) {
		if ((([[alertView textField] text].length) > 0) && (([[alertView textField] text].length) < MAXLENGTH)) {
		NSString *temp = [[alertView textField] text];
		string phrase = [temp cString];
		int a;
		int b = ([[alertView textField] text].length);
		for (a=0; a < b; a++) {
			char have;
			int tabe;
			have = phrase[a];
			for (tabe=0; tabe < 26; tabe++)
			{
				if (table[17][tabe] == have){
					phrase[a] = (table[0][tabe]);
					break; }
				else if (table[18][tabe] == have){
					phrase[a] = (table[1][tabe]);
					break; }
				else if (table[19][tabe] == have){
					phrase[a] = (table[2][tabe]);
					break; }
				else
					continue;
			}
		}
		const char* tempt = phrase.c_str();
		NSString *ans = [[NSString alloc] initWithCString: tempt];
		UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"Your decrypted message is:" message: ans delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
		[anse show];
		[anse release];
		[ans release];
		}
		else if (([[alertView textField] text].length) > MAXLENGTH) {
			UIAlertView *anse = [[UIAlertView alloc] initWithTitle:@"ERROR" message: @"The text you have entered is too long to be decrypted, try shortening the message." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
			[anse show];
			[anse release];
		}
	}
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


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

@end

Last edited by flyingguitar; 02-06-2010 at 05:07 PM.
flyingguitar is offline   Reply With Quote
Old 02-06-2010, 04:39 PM   #11 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

@OP, you from IPTF?
ZunePod is offline   Reply With Quote
Old 02-06-2010, 05:16 PM   #12 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

Quote:
Originally Posted by ZunePod View Post
@OP, you from IPTF?
yes I am :P
flyingguitar is offline   Reply With Quote
Old 02-07-2010, 05:40 PM   #13 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

sorry for the double post, but I figured while I was here I might as well ask...

what does the following line of code do?

Code:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I know what it does, but I don't really know how. in other words, I know the effect that the code has, I just want to know how it achives that effect.

sorry if that doesn't make sence, can somebody just break it down for me lol.
flyingguitar is offline   Reply With Quote
Old 02-07-2010, 07:48 PM   #14 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 41
Posts: 1,184
Default

I would definitely not be using STL strings in an iPhone app... in my experience, STL objects are not the way to go if you care about keeping memory consumption low.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 02-07-2010, 08:20 PM   #15 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

that's ok.
flyingguitar is offline   Reply With Quote
Old 02-07-2010, 08:46 PM   #16 (permalink)
Registered Member
 
flyingguitar's Avatar
 
Join Date: Feb 2010
Location: New Jersey
Posts: 10
Default

removed by author

Last edited by flyingguitar; 02-07-2010 at 10:14 PM.
flyingguitar is offline   Reply With Quote
Old 09-02-2010, 10:06 AM   #17 (permalink)
Registered Member
 
space_monkeys's Avatar
 
Join Date: Jun 2010
Location: Cairns, Queensland, AUSTRALIA
Age: 23
Posts: 17
Send a message via AIM to space_monkeys
Default Same here...

Quote:
Originally Posted by BrianSlick View Post
I generally don't worry about it unless I see my code on the right side.
I have both these exact same leaks in my iPad app in the exact same places and no idea why they're leaking. i've not altered the appdelegate. However, if i create a fresh new iPad app and add nothing, and run thru Instruments it has no leaks, even though the appdelegate header and implementation is the same as my other app. Has anyone seen this before? Is it a bug or something? I have both same lines of code affected as in the previous post, both also at 128Bytes each.

Hoping someone can shed some light on it for me...
__________________
"There are 10 kinds of people in the world: those who understand binary... and those who don't"
space_monkeys is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, memory leak

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: 258
23 members and 235 guests
@sandris, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, HDshot, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,754
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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