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 03-16-2010, 10:51 AM   #1 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
Default Out of memory

Code:
*** Assertion failure in +[Diary insertNewEntryIntoDiary:withString:withLocation:withDate:], /Users/iisword/Documents/Programs/TargetDiary/Classes/Diary.m:30
2010-03-16 10:49:51.346 TargetDiary[193:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to complete statment 'out of memory'.'
2010-03-16 10:49:51.347 TargetDiary[193:207] Stack: (
    30430299,
    2554115337,
    30514235,
    802580,
    12259,
    11576,
    16784,
    28837483,
    28833199,
    28808483,
    28826638,
    30214848,
    30211144,
    37713805,
    37714002,
    2789379,
    10364,
    10218
)
Thats the error I'm getting, what ways can I decrease my memory usage.
__________________
iisword is offline   Reply With Quote
Old 03-16-2010, 11:30 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,556
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

how many entries are you putting in the diary?
smithdale87 is offline   Reply With Quote
Old 03-16-2010, 12:58 PM   #3 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
Default

Quote:
Originally Posted by smithdale87 View Post
how many entries are you putting in the diary?
3 items going into diary(each a string): entry, location, and date.
__________________
iisword is offline   Reply With Quote
Old 03-16-2010, 01:03 PM   #4 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
Default

Here is the code I'm using

starts:
Code:
-(void)locationManager:(CLLocationManager *)manager 
   didUpdateToLocation:(CLLocation *)newLocation 
		  fromLocation:(CLLocation *)oldLocation
{
	NSLog(@"Setting Postition");
	self.currentPosition = newLocation;
	NSLog(@"Position Set");
	
	NSLog(@"Getting current date");
	NSDate *currentDate = [NSDate date];
	NSDateFormatter *format = [[NSDateFormatter alloc] init];
	[format setDateFormat:@"MMM dd, yyyy HH:mm"];
	NSString *dateString = [[NSString alloc] initWithString:[format stringFromDate:currentDate]];
	NSLog(@"Date Set");
	
	[format dealloc];
	
	float lat = currentPosition.coordinate.latitude; 
	float lon = currentPosition.coordinate.longitude;
	NSString *location;
	//test for east west south and north
	if (lat < 0 && lat < 0)
	{
		lat = -lat;
		lon = -lon;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°S   Longitude:%1.2f°W", lat, lon];
	}
	else if (lat> 0 && lon > 0)
	{
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°E", lat, lon];
	}
	else if (lon < 0 && lat > 0)
	{
		lon = -lon;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°W", lat, lon];
	}
	else
	{
		lat = -lat;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°E", lat, lon];
	}
	NSLog(location);
	NSLog(dateString);
	
	NSLog(@"Saving to database");
	[appDelegate setDiaryEntry:entry.text];
	[appDelegate setDateString:dateString];
	[appDelegate setLocation:location];
	[appDelegate addEntry];

	[save setHidden:NO];
	[active stopAnimating];
	[location dealloc];
	[dateString release];
	
}
goes to:
Code:
-(void)addEntry
{
	NSLog(@"Starting");
	NSInteger pk = [Diary insertNewEntryIntoDiary:diaryDatabase withString:diaryEntry withLocation:location withDate:dateString];
	Diary *newDiary = [[Diary alloc] initWithPrimaryKey:pk database:diaryDatabase];
		
	[diaries addObject:newDiary];
	NSLog(@"ended");
}

-(void)setDiaryEntry:(NSString *)newEntry
{
	diaryEntry = newEntry;
}

-(void)setDateString:(NSString *)newDate
{
	dateString = newDate;
}

-(void)setLocation:(NSString *)newLocation
{
	location = newLocation;
}
and finally
+(NSInteger)insertNewEntryIntoDiarysqlite3 *)diaryDatabase withStringNSString *)text withLocationNSString *)location withDateNSString *)date
{
NSLog(@"Method Started");
if (add_statement == nil)
{
static char *sql = "INSERT INTO diary(entry, location, date) VALUES (?,?,?)";
NSLog(@"sql created");
if (sqlite3_prepare_v2(diaryDatabase, sql, -1, &add_statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to complete statment '%s'.", sqlite3_errmsg(diaryDatabase));
}
}

sqlite3_bind_text(add_statement, 1, [text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(add_statement, 2, [location UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(add_statement, 3, [date UTF8String], -1 , SQLITE_TRANSIENT);

int success = sqlite3_step(add_statement);
if (success != SQLITE_ERROR)
{
return sqlite3_last_insert_rowid(diaryDatabase);
NSLog(@"ended successfully");
}
else
{
NSAssert1(0, @"Error occured in insertNewEntry with message '%s'.", sqlite3_errmsg(diaryDatabase));
return -1;
}
}[/code]
__________________
iisword is offline   Reply With Quote
Old 03-16-2010, 01:20 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Post

You'll have to read more about retain, release, and properties. I see you doing things like this:

Code:
	//destroy the "location" string, even if someone else is using
	//or retaining it.
	[location dealloc];
You should not call "dealloc" yourself except [super dealloc] at the end of your own dealloc method. You should be calling [location release] here instead. Same with [format dealloc].

Your setDiaryEntry, setDateString, setLocation methods are also very dangerous because they don't retain the data you send them. It may work now because you call addEntry right away, but If you try to access those pointers later (after someone else releases these strings) they may be invalid.

The more correct way to write them would be:
Code:
-(void)setDiaryEntry:(NSString *)newEntry{

	// do nothing if pointer is to the same entry
	if (newEntry != diaryEntry){ 

		[diaryEntry release]; //release old entry
		diaryEntry = [newEntry retain]; //retain new entry

	}
}
A better and easier way to handle this would be to use properties; whatever book you're using should have chapters on retain, release, and properties.
__________________

Free Games!

Last edited by smasher; 03-16-2010 at 01:22 PM.
smasher is offline   Reply With Quote
Old 03-17-2010, 09:26 PM   #6 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
Default So I changed my code around

Quote:
Originally Posted by smasher View Post
You'll have to read more about retain, release, and properties. I see you doing things like this:

Code:
	//destroy the "location" string, even if someone else is using
	//or retaining it.
	[location dealloc];
You should not call "dealloc" yourself except [super dealloc] at the end of your own dealloc method. You should be calling [location release] here instead. Same with [format dealloc].

Your setDiaryEntry, setDateString, setLocation methods are also very dangerous because they don't retain the data you send them. It may work now because you call addEntry right away, but If you try to access those pointers later (after someone else releases these strings) they may be invalid.

The more correct way to write them would be:
Code:
-(void)setDiaryEntry:(NSString *)newEntry{

	// do nothing if pointer is to the same entry
	if (newEntry != diaryEntry){ 

		[diaryEntry release]; //release old entry
		diaryEntry = [newEntry retain]; //retain new entry

	}
}
A better and easier way to handle this would be to use properties; whatever book you're using should have chapters on retain, release, and properties.
I'm noticing a leak from UITextView when the user clicks on it...

Also I changed the addEntry method to
Code:
addEntry:(NSString *) withLocation:(NSString *)withDate:(NSString *)
__________________
iisword is offline   Reply With Quote
Old 03-17-2010, 09:46 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by iisword View Post
I'm noticing a leak from UITextView when the user clicks on it...

Also I changed the addEntry method to
Code:
addEntry:(NSString *) withLocation:(NSString *)withDate:(NSString *)
You'll get a leak if you're retaining something and not releasing it when you're done with it. If you used the setDiaryEntry that I wrote above, or you used a property, then you should release newEntry in the dealloc method for that class.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-18-2010, 12:16 AM   #8 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 527
Default

I got rid of my set methods and tried using this way:
Code:
NSLog(@"Setting Postition");
	self.currentPosition = newLocation;
	NSLog(@"Position Set");
	
	NSLog(@"Getting current date");
	NSDate *currentDate = [NSDate date];
	NSDateFormatter *format = [[NSDateFormatter alloc] init];
	[format setDateFormat:@"MMM dd, yyyy HH:mm"];
	NSString *dateString = [[NSString alloc] initWithString:[format stringFromDate:currentDate]];
	NSLog(@"Date Set");
	
	NSString *entryString = [[NSString alloc] initWithString:entry.text];
	
	[entry release];
	[format release];
	[currentDate release];
	
	float lat = currentPosition.coordinate.latitude; 
	float lon = currentPosition.coordinate.longitude;
	NSString *location;
	
	if (lat < 0 && lat < 0)
	{
		lat = -lat;
		lon = -lon;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°S   Longitude:%1.2f°W", lat, lon];
	}
	else if (lat> 0 && lon > 0)
	{
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°E", lat, lon];
	}
	else if (lon < 0 && lat > 0)
	{
		lon = -lon;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°W", lat, lon];
	}
	else
	{
		lat = -lat;
		location = [[NSString alloc] initWithFormat:@"Latitude:%1.2f°N   Longitude:%1.2f°E", lat, lon];
	}
	NSLog(location);
	NSLog(dateString);
	
	NSLog(@"Saving to database");
	[appDelegate addEntry:entryString withLocation:location withDate:dateString];

	[save setHidden:NO];
	[active stopAnimating];
	[location release];
	[dateString release];
	[entryString release];
still getting the same error
__________________
iisword is offline   Reply With Quote
Reply

Bookmarks

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: 286
21 members and 265 guests
2WeeksToGo, ADY, apatsufas, ckgni, dacapo, Dani77, Fritzer, ghost, HDshot, headkaze, jakerocheleau, masc2279, mer10, mystic.purple, objch, Rudy, stoneage, tathaastu, themathminister, timle8n1, Zool
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,878
Threads: 89,224
Posts: 380,732
Top Poster: BrianSlick (7,129)
Welcome to our newest member, olga2000
Powered by vBadvanced CMPS v3.1.0

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