How to manage memory using classes in Objective-C?
- by Flipper
This is my first time creating an iPhone App and I am having difficulty with the memory management because I have never had to deal with it before.
I have a UITableViewController and it all works fine until I try to scroll down in the simulator. It crashes saying that it cannot allocate that much memory. I have narrowed it down to where the crash is occurring:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue or create a cell
UITableViewCellStyle style = UITableViewCellStyleDefault;
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"BaseCell"];
if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
NSString* crayon;
// Retrieve the crayon and its color
if (aTableView == self.tableView)
{
crayon = [[[self.sectionArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] getName];
}
else
{
crayon = [FILTEREDKEYS objectAtIndex:indexPath.row];
}
cell.textLabel.text = crayon;
if (![crayon hasPrefix:@"White"])
cell.textLabel.textColor = [self.crayonColors objectForKey:crayon];
else
cell.textLabel.textColor = [UIColor blackColor];
return cell;
}
Here is the getName method:
- (NSString*)getName
{
return name;
}
name is defined as:
@property (nonatomic, retain) NSString *name;
Now sectionArray is an NSMutableArray with instances of a class that I created Term in it.
Term has a method getName that returns a NSString*. The problem seems to be the part of where crayon is being set and getName is being called. I have tried adding autorelease, release, and other stuff like that but that just causes the entire app to crash before even launching.
Also if I do:
cell.textLabel.text = @"test"; //crayon;
/*if (![crayon hasPrefix:@"White"])
cell.textLabel.textColor = [self.crayonColors objectForKey:crayon];
else
cell.textLabel.textColor = [UIColor blackColor];*/
Then I get no error whatsoever and it all scrolls just fine.
Thanks in advance for the help!
Edit:
Here is the full Log of when I try to run the app and the error it gives when it crashes:
[Session started at 2010-12-29 04:23:38 -0500.]
[Session started at 2010-12-29 04:23:44 -0500.]
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 1429.
gdb-i386-apple-darwin(1430,0x778720) malloc: * mmap(size=1420296192) failed (error code=12)
error: can't allocate region
** set a breakpoint in malloc_error_break to debug
gdb stack crawl at point of internal error:
[ 0 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (align_down+0x0) [0x1222d8]
[ 1 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (xstrvprintf+0x0) [0x12336c]
[ 2 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (xmalloc+0x28) [0x12358f]
[ 3 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (dyld_info_read_raw_data+0x50) [0x1659af]
[ 4 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (dyld_info_read+0x1bc) [0x168a58]
[ 5 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (macosx_dyld_update+0xbf) [0x168c9c]
[ 6 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (macosx_solib_add+0x36b) [0x169fcc]
[ 7 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (macosx_child_attach+0x478) [0x17dd11]
[ 8 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (attach_command+0x5d) [0x64ec5]
[ 9 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (mi_cmd_target_attach+0x4c) [0x15dbd]
[ 10 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (captured_mi_execute_command+0x16d) [0x17427]
[ 11 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (catch_exception+0x41) [0x7a99a]
[ 12 ] /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/libexec/gdb/gdb-i386-apple-darwin (mi_execute_command+0xa9) [0x16f63]
/SourceCache/gdb/gdb-967/src/gdb/utils.c:1144: internal-error: virtual memory exhausted: can't allocate 1420296192 bytes.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
The Debugger has exited with status 1.The Debugger has exited with status 1.
Here is the backtrace that I get when I set the breakpoint for malloc_error_break:
#0 0x0097a68c in objc_msgSend ()
#1 0x01785bef in -[UILabel setText:] ()
#2 0x000030e0 in -[TableViewController tableView:cellForRowAtIndexPath:] (self=0x421d760, _cmd=0x29cfad8, aTableView=0x4819600, indexPath=0x42190f0) at /Volumes/Main2/Enayet/TableViewController.m:99
#3 0x016cee0c in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
#4 0x016c6a43 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
#5 0x016d954f in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow] ()
#6 0x016d08ff in -[UITableView layoutSubviews] ()
#7 0x03e672b0 in -[CALayer layoutSublayers] ()
#8 0x03e6706f in CALayerLayoutIfNeeded ()
#9 0x03e668c6 in CA::Context::commit_transaction ()
#10 0x03e6653a in CA::Transaction::commit ()
#11 0x03e6e838 in CA::Transaction::observer_callback ()
#12 0x00b00252 in __CFRunLoopDoObservers ()
#13 0x00aff65f in CFRunLoopRunSpecific ()
#14 0x00afec48 in CFRunLoopRunInMode ()
#15 0x00156615 in GSEventRunModal ()
#16 0x001566da in GSEventRun ()
#17 0x01689faf in UIApplicationMain ()
#18 0x00002398 in main (argc=1, argv=0xbfffefb0) at /Volumes/Main2/Enayet/main.m:14