Right now in my application there is a producer that outputs an array of data; the output from that producer is bound (using the "Bindings" in the XIB file) to a Table View in my window. The producer spits out data, and it shows up in the window, all is well.
Except that I need to modify the data being shown. The producer is a third-party app, so I can't modify it directly, so I need to create a filter object that sits between the two.
The object I created looks like this:
@interface testFilter: NSObject {
id output;
}
-(void)setInput:(id)s;
@end
I changed the binding so that the output from the producer goes to my input:
[myFilter bind:@"input" toObject:producer withKeyPath:@"output" options:0];
and my implementation looks like this:
-(id)init
{
self = [super init];
output = nil;
return self;
}
- (void)setInput:(id)newInput
{
int nEntries = (int)[newInput count];
id copiedArray = [NSMutableArray arrayWithCapacity:3];
for (id entry in newInput)
{
id copiedEntry = [entry mutableCopy];
// blah blah make some changes to copiedEntry
[copiedArray addObject:copiedEntry];
[copiedEntry release]; // I'm done with it, copiedArray added his own retain
}
[self setValue:copiedArray forKey:@"output"];
[copiedArray release]; // I'm done with it, setValue/output added a retain
}
But this is crashing with the error:
"malloc: *** error for object 0x108e00870: pointer being freed was not allocated"
...until I remove the [copiedArray release] line.
Am I wrong in thinking that I should be sending [copiedArray release]?
What else can I check / what is the recommended way to debug problems like this?
[newInput valueForKey:@"mutableCopy"]will do the same thing as most of thatforloop, which is to create an array (assumingnewInputis an array, since you didn't declare its type) of mutable copies of the input objects. You'll still need aforloop, but that loop will do nothing but make the changes that you commented out.-valueForKey:compensate for the fact that-mutableCopyreturns a retained object, or will that leak the copied entries?valueForKey:, this time gettingautorelease. That said, using KVC withmutableCopyis pretty hacky to start with, and doing the same withautoreleaseis even moreso.)