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 03-14-2011, 01:45 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 54
amatoria is on a distinguished road
Default Crash on NSPlaceholderString initWithString???

I have created my user login view (username/password) in UITableView with 2 cells having username and password. Both cells are created by adding UITextField as subview in contentview of the cell. Like this...

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = nil;
	
	if (indexPath.section == 0) {
		NSString *cellIdentifier = @"LoginCellIdentifier";

		cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
			UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
			leftLabel.backgroundColor = [UIColor clearColor];
			leftLabel.tag = 1;
			[cell.contentView addSubview:leftLabel];
			[leftLabel release];

			UITextField *valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 400, 35)];
			valueTextField.tag = 2;
			valueTextField.delegate = self;
			[cell.contentView addSubview:valueTextField];
			[valueTextField release];
		}

		if (indexPath.row == 0) {	// User name
			UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
			lblText.text = @"Username: ";

			UITextField *userNameField = (UITextField *)[cell.contentView viewWithTag:2];
			userNameField.placeholder = @"Enter your username here";		
		}
		else {	// Pass word
			UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
			lblText.text = @"Password: ";

			UITextField *passwordField = (UITextField *)[cell.contentView viewWithTag:2];
			passwordField.placeholder = @"Enter your password here";
			passwordField.secureTextEntry = YES;
		}
	}

	return cell;
}
Now, when I click in First cell->second cell->First cell back then the app crashes with following crash log on console..

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e4abe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f9f5c2 objc_exception_throw + 47
2 CoreFoundation 0x00e03628 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x00e0359a +[NSException raise:format:] + 58
4 Foundation 0x0004e1a8 -[NSPlaceholderString initWithString:] + 105
5 MobileLearning 0x000038c7 -[AccountLoginViewController getValueForTextField:] + 552
6 MobileLearning 0x00003699 -[AccountLoginViewController textFieldDidEndEditing:] + 43
7 UIKit 0x00359723 -[UITextField _resignFirstResponder] + 388
8 UIKit 0x0038d29a -[UIResponder resignFirstResponder] + 455
9 UIKit 0x0035522f -[UITextField resignFirstResponder] + 79
10 UIKit 0x0038d457 -[UIResponder becomeFirstResponder] + 280
.
.
------------------------------------------------------------------------------

This code below is UITextField delegate methods.. (if it makes any sense with respect to the crash log)
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[self getValueForTextField:textField];	// Get the value for current field holding the keyboard.

	Database *db = [[Database alloc] initDatabase];

	UserInfoRec *userInfoRec = (UserInfoRec*)[db getUserInfo:userName andPassword:password];

	if (userInfoRec != nil) {
		[[UserLogin sharedInstance] setCurrentUserInfo:userInfoRec];
		[textField resignFirstResponder];
	}
	
	[db release];

	return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	// Get value for text field just finished editing.
	[self getValueForTextField:textField];
}

- (void)getValueForTextField:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    NSLog(@"Row %d just finished editing with the value %@",textFieldIndexPath.row,textField.text);
	
	if (textFieldIndexPath.row == 0) {
		if (userName != nil) {
			[userName release];
		}
		userName = [[NSString alloc] initWithString:textField.text];
	}
	else {
		if (password != nil) {
			[password release];
		}
		password = [[NSString alloc] initWithString:textField.text];
	}
}
Please help me identify the reason for this crash. I don't know what's wrong in my placeholder string for the text field in the cell.

Thanks in advance..
-Ashok
amatoria is offline   Reply With Quote
Old 03-14-2011, 01:54 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

The problem isn't immediately jumping out at me, though I do notice that you aren't using properties. You should. This:

Code:
if (userName != nil) {
   [userName release];
}
...is just a horrible approach.

The error message is possibly indicating a memory management problem.
__________________
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 03-14-2011, 02:49 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 54
amatoria is on a distinguished road
Default username/password as property didn't work

Quote:
Originally Posted by BrianSlick View Post
The problem isn't immediately jumping out at me, though I do notice that you aren't using properties. You should. This:

Code:
if (userName != nil) {
   [userName release];
}
...is just a horrible approach.

The error message is possibly indicating a memory management problem.
Thanks man for your time..

Although I don't see any issue my earlier code but I tried what you suggested.. making username and password fields as property.

Now the .h file looks like
Code:
@interface AccountLoginViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
	IBOutlet UITableView *tableView;

	@private
	NSString *userName;
	NSString *password;
}

@property (nonatomic, retain) IBOutlet UITableView *tableView;

@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSString *password;

@end
and .m file have their synthesize method..
Code:
@synthesize tableView;
@synthesize userName;
@synthesize password;
and replaced each userName and password call with self.userName and self.password in the .m file code.

But the app still manage to crash with same crash log !!!!!! I have already tried clean build targets.

Please check this again..
amatoria is offline   Reply With Quote
Old 03-14-2011, 02:52 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

Post the new version of your code. Don't make me assume that you replaced everything correctly, because I won't do that.
__________________
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 03-14-2011, 03:12 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 54
amatoria is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Post the new version of your code. Don't make me assume that you replaced everything correctly, because I won't do that.
Code:
- (void)dealloc {
	[self.userName release];
	[self.password release];
    [super dealloc];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = nil;
	
	if (indexPath.section == 0) {
		NSString *cellIdentifier = @"LoginCellIdentifier";

		cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
			UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
			leftLabel.backgroundColor = [UIColor clearColor];
			leftLabel.tag = 1;
			[cell.contentView addSubview:leftLabel];
			[leftLabel release];

			UITextField *valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 400, 35)];
			valueTextField.tag = 2;
			valueTextField.delegate = self;
			[cell.contentView addSubview:valueTextField];
			[valueTextField release];
		}

		if (indexPath.row == 0) {	// User name
			UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
			lblText.text = @"Username: ";

			UITextField *userNameField = (UITextField *)[cell.contentView viewWithTag:2];
			userNameField.placeholder = @"Enter your username here";		
		}
		else {	// Pass word
			UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
			lblText.text = @"Password: ";

			UITextField *passwordField = (UITextField *)[cell.contentView viewWithTag:2];
			passwordField.placeholder = @"Enter your password here";
			passwordField.secureTextEntry = YES;
		}
	}

	return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[self getValueForTextField:textField];	// Get the value for current field holding the keyboard.

	Database *db = [[Database alloc] initDatabase];

	UserInfoRec *userInfoRec = (UserInfoRec*)[db getUserInfo:self.userName andPassword:self.password];

	if (userInfoRec != nil) {
		[[UserLogin sharedInstance] setCurrentUserInfo:userInfoRec];
		[textField resignFirstResponder];
	}
	
	[db release];

	return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	// Get value for text field just finished editing.
	[self getValueForTextField:textField];
}

- (void)getValueForTextField:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    NSLog(@"Row %d just finished editing with the value %@",textFieldIndexPath.row,textField.text);
	
	if (textFieldIndexPath.row == 0) {
		if (self.userName != nil) {
			[self.userName release];
		}
		self.userName = [[NSString alloc] initWithString:textField.text];
	}
	else {
		if (self.password != nil) {
			[self.password release];
		}
		self.password = [[NSString alloc] initWithString:textField.text];
	}
}
As requested, I've posted all the code using username/password fields.

Since the app crashes in placeholder string and as you can see that I've assigned some placeholder string to textfield there so do you see anything wrong in that code in cellForRowAtIndexPath?

PS: I'm a C++ guy and comparatively new to objective-C.

And, also please notice the second line of crash log below, just before crash.. It has (null) as the output !!! I didn't type in anything in both text field so output text should have been be same for both rows.

(complete crash log below)

[Session started at 2011-03-15 01:16:09 +0530.]
2011-03-15 01:16:14.632 MobileLearning[1287:207] Row 0 just finished editing with the value
2011-03-15 01:16:15.247 MobileLearning[1287:207] Row 1 just finished editing with the value (null)
2011-03-15 01:16:15.250 MobileLearning[1287:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e4bbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00fa05c2 objc_exception_throw + 47
2 CoreFoundation 0x00e04628 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x00e0459a +[NSException raise:format:] + 58
4 Foundation 0x0004f1a8 -[NSPlaceholderString initWithString:] + 105
5 MobileLearning 0x00003773 -[AccountLoginViewController getValueForTextField:] + 562
6 MobileLearning 0x0000353b -[AccountLoginViewController textFieldDidEndEditing:] + 43
7 UIKit 0x0035a723 -[UITextField _resignFirstResponder] + 388
8 UIKit 0x0038e29a -[UIResponder resignFirstResponder] + 455
9 UIKit 0x0035622f -[UITextField resignFirstResponder] + 79
10 UIKit 0x0038e457 -[UIResponder becomeFirstResponder] + 280
11 UIKit 0x00550e09 -[UITextInteractionAssistant setFirstResponderIfNecessary] + 208
12 UIKit 0x00553a8a -[UITextInteractionAssistant oneFingerTap:] + 1676
13 UIKit 0x0054a9c7 -[UIGestureRecognizer _updateGestureWithEvent:] + 727
14 UIKit 0x005469d6 -[UIGestureRecognizer _delayedUpdateGesture] + 47
15 UIKit 0x0054cfa5 _UIGestureRecognizerUpdateObserver + 584
16 UIKit 0x0054d18a _UIGestureRecognizerUpdateGesturesFromSendEvent + 51
17 UIKit 0x002e86b4 -[UIWindow _sendGesturesForEvent:] + 1292
18 UIKit 0x002e3f87 -[UIWindow sendEvent:] + 105
19 UIKit 0x002c737a -[UIApplication sendEvent:] + 447
20 UIKit 0x002cc732 _UIApplicationHandleEvent + 7576
21 GraphicsServices 0x016eea36 PurpleEventCallback + 1550
22 CoreFoundation 0x00e2d064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FU NCTION__ + 52
23 CoreFoundation 0x00d8d6f7 __CFRunLoopDoSource1 + 215
24 CoreFoundation 0x00d8a983 __CFRunLoopRun + 979
25 CoreFoundation 0x00d8a240 CFRunLoopRunSpecific + 208
26 CoreFoundation 0x00d8a161 CFRunLoopRunInMode + 97
27 GraphicsServices 0x016ed268 GSEventRunModal + 217
28 GraphicsServices 0x016ed32d GSEventRun + 115
29 UIKit 0x002d042e UIApplicationMain + 1160
30 MobileLearning 0x00001bcc main + 102
31 MobileLearning 0x00001b5d start + 53
)
terminate called after throwing an instance of 'NSException'
amatoria is offline   Reply With Quote
Old 03-14-2011, 03:18 PM   #6 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 54
amatoria is on a distinguished road
Default Issue is with password text field only..

Issue is with second row only (password row). Since the app crashes when I loose focus from the second row text field... Either by clicking in first row text or by keyboard disappear option on bottom right of keyboard.
No crash when I loose focus from first row (username).
amatoria is offline   Reply With Quote
Old 03-14-2011, 03:29 PM   #7 (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

Just use the instance variables in dealloc.

Code:
- (void)dealloc {
	[self.userName release];
	[self.password release];
    [super dealloc];
}
This is a somewhat dangerous approach. The returned value cannot be nil.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = nil;
	
	if (indexPath.section == 0)

...

	return cell;
}
I'm curious about this method:

Code:
UserInfoRec *userInfoRec = (UserInfoRec*)[db getUserInfo:self.userName andPassword:self.password];
Don't do this:

Code:
if (self.userName != nil) {
   [self.userName release];
}
Or this:

Code:
if (self.password != nil) {
   [self.password release];
}
The existence of an object does not mean the object should be released.

These are leaks:

Code:
self.userName = [[NSString alloc] initWithString:textField.text];
...
self.password = [[NSString alloc] initWithString:textField.text];
Let's take the initWithString out of the equation and see what happens:

Code:
self.userName = textField.text;
self.password = textField.text;
__________________
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 03-14-2011, 10:18 PM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 54
amatoria is on a distinguished road
Default Solved...

Thanks a lot man..

All your points well said.
And, this was THE change that solved the issue. (Memory leak of userName/password)
Code:
- (void)getValueForTextField:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    NSLog(@"Row %d just finished editing with the value %@", textFieldIndexPath.row, textField.text);

	if (textFieldIndexPath.row == 0) {
		self.userName = textField.text;
	}
	else {
		self.password = textField.text;
	}
}
I think I don't need to release them since the class object doesn't own them. Or since I obtained these string using 'retain' property so i should explicitly release both??? If so then where do I release both?
amatoria is offline   Reply With Quote
Old 03-14-2011, 10:30 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

All retain and copy properties should be released in dealloc. See the properties link in my signature for guidance.

Leaks do not cause crashes. Something was being over-released (or under-retained). I can't quite put my finger on it, but it was probably the releases I mentioned before as something not to do.
__________________
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
crash, initwithstring, nsplaceholderstring

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: 364
11 members and 353 guests
Absentia, akphyo, apatsufas, BinHex, blueorb, fredidf, iGamesDev, Kirkout, MarkC, mottdog, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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