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
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.
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.
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.
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.
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"