I'm getting weird EXC_BAD_ACCESS errors. I thought that I didn't need to retain objects if I was didn't need them after the method exited, but I want to double-check that. In the following, do I need to retain "tData"?
-(void)m开发者_开发问答yMethod:(UITouch*)touch{
TouchData *tData = (TouchData *)CFDictionaryGetValue(datasByUITouch, touch);
[tData doSomething];
}
I think according to The Rules, what you're doing there doesn't require you to retain the tData object that scope. You're not doing an init, alloc, new or copy, instead you're using one of TouchData's methods to return some data, meaning that the method of TouchData that you're accessing is responsible for releasing the object.
Is the object being returned a CoreFoundation object? If so, see this from the Apple Docs (http://developer.apple.com/mac/library/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-SW1)
If you receive an object from any Core Foundation function other than a creation or copy function—such as a Get function—you do not own it and cannot be certain of the object’s life span. If you want to ensure that such an object is not disposed of while you are using it, you must claim ownership (with the CFRetain function). You are then responsible for relinquishing ownership when you have finished with it.
Right, no need to retain in this method. Looks like touch was released before this method was called.
How was the CFDictionary created? If it wasn't created with instructions to retain values, then the dictionary won't retain touch. If that's the case, pass kCFTypeDictionaryValueCallBacks (CFValueCallBacks) as the valueCallBacks argument of the CFDictionaryCreate (or CFDictionaryCreateMutable).
精彩评论