Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 16, 2007, 04:46 PM   #1
stadidas
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Problem Deleting From NSMutableArray

Hi everyone.

I'm currently having a big problem with an NSMutableArray. At the moment I can addobjects to it, and they display in a NSTableView perfectly. When I remove them they are removed from the array, but when I try to open the sheet containing the tableView I get an error like this: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)

If I then add another object to the array, the tableView works perfectly again. If I do not add another object and quit, I get this sort of error:
AssetMonitor [DEBUG](3206,0xa000ed88) malloc: *** Deallocation of a pointer not malloced: 0xbfffe270; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug

These are the methods I have for adding and removing:
Code:
- (void)addLog:(Log *)l
{
	[logs addObject:l];
}

- (void)removeObjectIdenticalTo:(id)anObject
{
	[logs removeObjectIdenticalTo:anObject];
}
I've been playing around for a few hours and haven't been able to come up with a fix. Any help appreciated with this one. Thanks.
stadidas is offline   0 Reply With Quote
Old Jan 16, 2007, 05:57 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Please post more code. Sounds like you're not using memory management correctly. And remember arrays start with an index of 0 for their first item.
kainjow is offline   0 Reply With Quote
Old Jan 16, 2007, 07:22 PM   #3
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
I suspect it's memory management too.
Here are the methods from MyDocument:
Code:
- (void)add:(double)d:(NSString *)s
{
	Log *l = [[Log alloc] init];
	[l setDescription:s];
	[l setAmount:d];
	[self doAdd:d:s:l];
}

- (void)doAdd:(double)d:(NSString *)s:(Log *)l
{
	[self addAmount:d];
	[self addLog:l];
	[tableView reloadData];
	
	NSUndoManager *undo = [self undoManager];
	[[undo prepareWithInvocationTarget:self]
		removeLog:l];
	
	[[undo prepareWithInvocationTarget:self]
		subtractAmount:d];
	
	if (![undo isUndoing]) {
		[undo setActionName:@"Add to Balance"];
	}
}

- (void)addAmount:(double)d
{
	double tempBalance = [myBalance doubleValue];
	double newBalance = tempBalance + d;
	[detailsController setMyBalance:newBalance];
}

- (void)addLog:(Log *)l
{
	[detailsController addLog:l];
}

- (void)removeLog:(id)anObject
{
	[detailsController removeObjectIdenticalTo:anObject];
}

- (void)subtract:(double)d:(NSString *)s
{
	Log *l = [[Log alloc] init];
	[l setDescription:s];
	[l setAmount:d];
	[self doSubtract:d:s:l];
}

- (void)doSubtract:(double)d:(NSString *)s:(Log *)l
{
	[self subtractAmount:d];
	[self addLog:l];
	[tableView reloadData];
	
	NSUndoManager *undo = [self undoManager];
	[[undo prepareWithInvocationTarget:self]
		removeLog:l];
	
	[[undo prepareWithInvocationTarget:self]
		addAmount:d];
	
	if (![undo isUndoing]) {
		[undo setActionName:@"Subtract From Balance"];
	}
	
}

- (void)subtractAmount:(double)d
{
	double tempBalance = [myBalance doubleValue];
	double newBalance = tempBalance - d;
	[detailsController setMyBalance:newBalance];
}
The add: and subtract: methods are called from when a sheet is closed which passes the values. detailsController refers to an instance of a class called details which stores the NSMutableArray. The problem I described in the first post came from calling the add: method, and then hitting undo, which I expected to remove the instance of Log that had been stored.
Let me know if you want to see anything else.

Last edited by stadidas; Jan 16, 2007 at 07:25 PM. Reason: Added detail.
stadidas is offline   0 Reply With Quote
Old Jan 16, 2007, 08:19 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Well when you make your Log objects, you're leaking memory:

Code:
Log *l = [[Log alloc] init];
[l setDescription:s];
[l setAmount:d];
[self doAdd:d:s:l];
Should be

Code:
Log *l = [[Log alloc] init];
[l setDescription:s];
[l setAmount:d];
[self doAdd:d:s:l];
[l release];
(unless you were on 10.5 with GC enabled ;)
kainjow is offline   0 Reply With Quote
Old Jan 16, 2007, 09:01 PM   #5
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
I might point out that it's horrible programming style to use single character names for variables/arguments. Makes for really difficult reading later.

Note: If you're trying to make your code hard to read for some reason, ignore this.
mduser63 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
added objects to nsmutablearray, yet the array is null johnmerlino Mac Programming 4 Nov 11, 2011 10:50 PM
Trying to convert object to NSMutableArray johnmerlino iPhone/iPad Programming 4 Nov 11, 2011 03:08 AM
How To Select Random UIButton From NsMutablearray mandude iPhone/iPad Programming 13 May 11, 2010 07:51 PM
Strip blank strings from NSMutableArray pcwiz Mac Programming 1 Aug 10, 2009 10:48 PM
replace value from nsmutablearray estupefactika iPhone/iPad Programming 3 Feb 23, 2009 02:11 PM


All times are GMT -5. The time now is 10:55 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC