Question

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?

Was it helpful?

Solution

id copiedArray = [NSMutableArray arrayWithCapacity:3];

This will create an autoreleased object. You shouldn't release autoreleased objects.

Either remove your release call, or change that line to this:

id copiedArray = [[NSMutableArray alloc] initWithCapacity:3];

That being said, consider using Automatic Reference Counting (ARC).


From Advanced Memory Management Programming Guide:

You own any object you create

You create an object using a method whose name begins with alloc, new, copy, or mutableCopy (for example, alloc, newObject, or mutableCopy).

You can take ownership of an object using retain

A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. You use retain in two situations: (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; and (2) To prevent an object from being invalidated as a side-effect of some other operation.

When you no longer need it, you must relinquish ownership of an object you own

You relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.

You must not relinquish ownership of an object you do not own

This is just corollary of the previous policy rules, stated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top