Objective-C 中 NSDictionary 神秘发布?
我正在为 Quartz Composer 的自定义补丁做最后的润色。截至目前,我几乎已经从补丁中删除了所有内容,当我尝试 NSLog 一个 ivar 的 NSDictionary 值时,它崩溃了,告诉我访问错误,并且在我分配它时的最后一次执行中工作得很好。
我的代码如下所示:
- (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;
}
我可以看到日志显示所有三个 NSLog 行都完美工作,直到 self.inputBoutData 有输入。然后,我看到 self.inputBoutData 已成功复制到循环的最后一个 NSLog 行中的 lastBoutData 。
在下一次运行execute:atTime:withArguments:时,self.inputBoutData仍然是满的,但是lastBoutData又是空白的!我不明白这是怎么发生的。然后,它又运行一个循环,就像上一次一样,并成功将 self.inputBoutData 复制到 lastBoutData,并再次记录。下一次,我在第二个 NSLog 语句之前得到了 BAD ACCESS 。
我收到一些错误消息,告诉我 lastBoutData 不是 NSDictionary,所以出于绝望,我添加了 [lastBoutData keep],并且它不会崩溃。我不会释放这个 ivar,所以我不确定为什么我必须保留它。我在许多其他补丁中对其他 ivars 做了非常类似的事情,没有任何问题。我可能会错过什么?为什么这个东西会释放到我身上,或者这就是正在发生的事情?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dictionaryWithDictionary:
返回一个自动释放的字典。由于您的财产没有(保留),所以没有任何东西可以保留它。因此,您以前的字典被取消引用并泄漏,并且您的新字典不会保留。考虑:
或者按原样使用您的代码并将保留添加到属性中。
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:
Or use your code as is and add retain to the property.