1

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?

4
  • 2
    The recommended way is to use ARC and never explicitly call release at all. Commented May 15, 2013 at 19:53
  • 1
    Incidentally, [newInput valueForKey:@"mutableCopy"] will do the same thing as most of that for loop, which is to create an array (assuming newInput is an array, since you didn't declare its type) of mutable copies of the input objects. You'll still need a for loop, but that loop will do nothing but make the changes that you commented out. Commented May 15, 2013 at 20:31
  • 1
    @PeterHosey, will -valueForKey: compensate for the fact that -mutableCopy returns a retained object, or will that leak the copied entries? Commented May 16, 2013 at 0:31
  • @BrentRoyal-Gordon: Good question. I don't know, but my guess would be no. (And if not, you could hack around it with another valueForKey:, this time getting autorelease. That said, using KVC with mutableCopy is pretty hacky to start with, and doing the same with autorelease is even moreso.) Commented May 16, 2013 at 1:56

1 Answer 1

9
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.