سؤال

I'm working on the finishing touches of a custom patch for Quartz Composer. As of right now, I have almost everything stripped out of the patch, and it's crashing telling me BAD ACCESS when I try to NSLog a NSDictionary value that is an ivar, and that worked perfectly in the last execution when I assigned it.

My code looks like this:

- (BOOL) startExecution:(id<QCPlugInContext>)context
{
   lastBoutData = [[NSDictionary alloc] init ];

   return YES;
}

- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{

    NSLog(@"self.inputBoutData: %@", self.inputBoutData);
    NSLog(@"lastBoutData: %@", lastBoutData);


    // have new data, put it on the output
    self.lastBoutData = [NSDictionary dictionaryWithDictionary:self.inputBoutData];
    NSLog(@"assigned: %@", lastBoutData);

    return YES;
}

I can see the log shows that all three NSLog lines work perfectly until self.inputBoutData has input. Then, I see that self.inputBoutData is successfully copied to lastBoutData in the last NSLog line of the loop.

In the very next run of execute:atTime:withArguments:, self.inputBoutData is still full, but lastBoutData is blank again!!! I'm can't see how that can happen. Then, it runs one more loop, just like the last, and successfully copies the self.inputBoutData to lastBoutData, and it's logged again. The next time through, I get BAD ACCESS just before the second NSLog statement.

I was getting some error messages that told me that lastBoutData wasn't an NSDictionary, so out of desperation, I added a [lastBoutData retain], and it doesn't crash. I'm not releasing this ivar, so I'm not sure why I have to retain it. I do very similar things with other ivars in many other patches with no issues. What could I be missing? Why is this thing releasing on me, or is that even what is happening?

هل كانت مفيدة؟

المحلول

self.lastBoutData = [NSDictionary dictionaryWithDictionary:self.inputBoutData];

dictionaryWithDictionary: returns an autoreleased dictionary. And since your property was not (retain) nothing was retaining it. So your previous dictionary is de-referenced and leaked and your new dictionary is not retained.

consider:

[lastBoutData release];
lastBoutData = [[NSDictionary alloc] initWithDictionary:self.inputBoutData];

Or use your code as is and add retain to the property.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top