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-01-2011, 01:54 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Question Strangest Problem I Have Seen Please Help

Hello Everyone. This is one the strangest problems I've come across. I can't seem to find an explanation. The application crashes when NSLog("It works!") is commented out and gives an EXC_BAD_ACCESS error. But when I uncomment this line. The application runs as it should.

This method is supposed to obtain car data from an sql file and set the columns as the properties of a singleton. The information is displayed in another view. The line in bold is the one giving me trouble.

Code:
-(void) obtainCarData{
	
	//Get the file path of the database to open it
	//databaseName = @"Car Database.sqlite";
	//NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	//NSString *documentsDir = [documentPaths objectAtIndex:0];
	//databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
	
	sqlite3 *database;
	
	NSString *userCarType = [NSString stringWithFormat:@"Compact"];
	NSString *userCarYear = [NSString stringWithFormat:@"2010"];
	
	if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
		
		NSString *sqlStatement = [NSString stringWithFormat:@"select * from CarDatabase where Type = '%@' and Year = '%@'",userCarType,userCarYear];
		
		sqlite3_stmt *compiledStatement;
				 
		 int num = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL);
		 if(num == SQLITE_OK) {
			NSLog(@"It Works!");
		 }
		 //else{
		 //printf( "Error LOG: %s", sqlite3_errmsg(database) );
		// NSLog(@"num:%d", num);
		// }
		 
		if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
			
			while (sqlite3_step(compiledStatement) == SQLITE_ROW) {

				[Car userCar].index = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 0)];
				[Car userCar].carModel = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiledStatement, 1)];
				[Car userCar].carClass= [NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiledStatement, 2)];
				[Car userCar].carYear = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 3)];
				[Car userCar].wheelRadius= [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 4)];
				[Car userCar].differential = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 5)];
				[Car userCar].engineSize = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 6)];
				[Car userCar].cylinders = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 7)];
				[Car userCar].gears = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 8)];
				[Car userCar].gearRatio1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 9)];
				[Car userCar].gearRatio2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 10)];
				[Car userCar].gearRatio3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 11)];
				[Car userCar].gearRatio4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 12)];
				[Car userCar].gearRatio5 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 13)];
				[Car userCar].gearRatio6 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 14)];
				[Car userCar].gearMax1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 15)];
				[Car userCar].gearMax2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 16)];
				[Car userCar].gearMax3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 17)];
				[Car userCar].gearMax4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 18)];
				[Car userCar].gearMax5 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 19)];
				[Car userCar].gearMin1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 20)];
				[Car userCar].gearMin2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 21)];
				[Car userCar].gearMin3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 22)];
				[Car userCar].gearMin4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 23)];
				[Car userCar].gearMin51 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 24)];
				
				sqlite3_finalize(compiledStatement);
				sqlite3_close(database);
			}
		}
	}
}
Could someone please tell me what is going on? I'm confused as to why it is not letting me remove the NSLog.
Mystery is offline   Reply With Quote
Old 08-01-2011, 02:38 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

The EXC_BAD_ACCESS error usually means there's a Memory issue and that's why it's crashing. I had this happen once as well with the same setup (once a NSLog statement was removed it crashed) and although I can't really explain why this happens I did fix it by using the Instruments tool (checking for Zombies) and found the object that was being double released in my case and causing the issue. The only thing I could think why the NSLog made a difference is that the memory was being shifted around so it didn't cause a problem at that given moment but I'm pretty sure the app would have still crashed later down the line as that one item was still being double called and taking up a piece of the memory.
mavrik5150 is offline   Reply With Quote
Old 08-01-2011, 02:42 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

That seems unlikely.

What exactly does your code that crashes look like? Can you post it here (instead of general instructions on how to modify the non-crashing code)?

Also, exactly where does it crash? Have you stepped through it, line by line, in the debugger?

EDIT: To clarify, my response is to the OP, not in response to mavrik5150's response. Just in case any part of it might have seemed otherwise...
__________________
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 08-01-2011, 02:52 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Default

Quote:
Originally Posted by mavrik5150 View Post
The EXC_BAD_ACCESS error usually means there's a Memory issue and that's why it's crashing. I had this happen once as well with the same setup (once a NSLog statement was removed it crashed) and although I can't really explain why this happens I did fix it by using the Instruments tool (checking for Zombies) and found the object that was being double released in my case and causing the issue. The only thing I could think why the NSLog made a difference is that the memory was being shifted around so it didn't cause a problem at that given moment but I'm pretty sure the app would have still crashed later down the line as that one item was still being double called and taking up a piece of the memory.
Thanks for the suggestion! I do realize that the EXC_BAD_ACCESS is a memory issue. I have also used the NSZombie to see if, as you said, i am releasing an object twice. The NSZombie highlighted this line.

while (sqlite3_step(compiledStatement) == SQLITE_ROW) {

And I know that NSZombie does not always select the right line but I don't see any sources of error around there. And it is not just the NSLog that is required .

int num = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL);
if(num == SQLITE_OK) {
NSLog(@"It Works!");
}

This whole section is required for some reason. If I leave this out, the program crashes. If i leave it in, the program does run as it is intended to so I am not sure if what you said about the memory shift is true. Although I could be wrong. So I still don't know how to fix this.
Mystery is offline   Reply With Quote
Old 08-01-2011, 03:09 PM   #5 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

I don't know if this will solve your problem but you are finalizing the SQL statement and you are closing the database connection in the while loop, in which you are reading the results returned from the executed statement. Try changing the code to this:

Code:
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
		
		NSString *sqlStatement = [NSString stringWithFormat:@"select * from CarDatabase where Type = '%@' and Year = '%@'",userCarType,userCarYear];
		
		sqlite3_stmt *compiledStatement;
				 
		 int num = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL);
		 if(num == SQLITE_OK) {
			NSLog(@"It Works!");
		 }
		 //else{
		 //printf( "Error LOG: %s", sqlite3_errmsg(database) );
		// NSLog(@"num:%d", num);
		// }
		 
		if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
			
			while (sqlite3_step(compiledStatement) == SQLITE_ROW) {

				[Car userCar].index = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 0)];
				[Car userCar].carModel = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiledStatement, 1)];
				[Car userCar].carClass= [NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiledStatement, 2)];
				[Car userCar].carYear = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 3)];
				[Car userCar].wheelRadius= [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 4)];
				[Car userCar].differential = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 5)];
				[Car userCar].engineSize = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 6)];
				[Car userCar].cylinders = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 7)];
				[Car userCar].gears = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 8)];
				[Car userCar].gearRatio1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 9)];
				[Car userCar].gearRatio2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 10)];
				[Car userCar].gearRatio3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 11)];
				[Car userCar].gearRatio4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 12)];
				[Car userCar].gearRatio5 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 13)];
				[Car userCar].gearRatio6 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 14)];
				[Car userCar].gearMax1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 15)];
				[Car userCar].gearMax2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 16)];
				[Car userCar].gearMax3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 17)];
				[Car userCar].gearMax4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 18)];
				[Car userCar].gearMax5 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 19)];
				[Car userCar].gearMin1 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 20)];
				[Car userCar].gearMin2 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 21)];
				[Car userCar].gearMin3 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 22)];
				[Car userCar].gearMin4 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 23)];
				[Car userCar].gearMin51 = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 24)];
				
				
				
			}
		}
         sqlite3_finalize(compiledStatement);
	}
sqlite3_close(database);
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 08-01-2011, 03:11 PM   #6 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
That seems unlikely.

What exactly does your code that crashes look like? Can you post it here (instead of general instructions on how to modify the non-crashing code)?

Also, exactly where does it crash? Have you stepped through it, line by line, in the debugger?

EDIT: To clarify, my response is to the OP, not in response to mavrik5150's response. Just in case any part of it might have seemed otherwise...

The problem was solved. I looked into the while loop as indicated by the NSZombie line by line and realized that the finalize and close statement was in the wrong place. It should have been outside the while loop. I still wonder why it would work with that snippet of code though... But thanks for your help guys.

@apatsufas: Yes! That is exactly what I did! And it fixed the problem. You found it as soon as I fixed it haha. Thanks.

Last edited by Mystery; 08-01-2011 at 03:14 PM.
Mystery is offline   Reply With Quote
Old 08-01-2011, 03:22 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

It's probably a side effect of the SQLite implementation.

In your initial code with the NSLog that worked, you were actually preparing the statement twice (once to log the result, then again to actually use it). Perhaps SQLite maps these both to the same actual statement internally, so that calling finalize too soon on one doesn't actually destroy the underlying compiled statement? (even though you were calling finalize on every row... hmm... how many rows do you actually have? two?)

Just guessing, based on your observed behavior, though.
__________________
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 08-01-2011, 04:48 PM   #8 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
It's probably a side effect of the SQLite implementation.

In your initial code with the NSLog that worked, you were actually preparing the statement twice (once to log the result, then again to actually use it). Perhaps SQLite maps these both to the same actual statement internally, so that calling finalize too soon on one doesn't actually destroy the underlying compiled statement? (even though you were calling finalize on every row... hmm... how many rows do you actually have? two?)

Just guessing, based on your observed behavior, though.
I think I understand what you're trying to explain. I actually have 9 rows. The one in which I told it to search for is on the third row. So although what you proposed is a good estimation, since it's on the third row it may be false.

I repeated the problem again and I caught a glimpse of the UIView before it closed for a crash (which it did not do before) and it displayed the correct information right before it closed.

The information I gave in the beginning and now may not be enough information to determine exactly what happened here. It is still a good puzzle nonetheless. At least the problem is solved... Thanks!
Mystery is offline   Reply With Quote
Reply

Bookmarks

Tags
database, memory, nslog, singleton, sql

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: 393
15 members and 378 guests
7twenty7, blasterbr, buggen, chiataytuday, dedeys78, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,902
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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