I'm trying to understand this from a memory management point of view: In one class, I'm making a helper method that will create an NSDictionary object for me.

+(NSDictionary*) getTheDictionary{
    return [[[NSDictionary alloc] initWithObjectsAndKeys:
        @"value", @"key", nil] autorelease];
}

From another class, I use the method.

NSDictionary* theDictionary = [HelperClass getTheDictionary];

Is it enough to just have "autorelease" in the return statement? Do I also need an autorelease on theDictionary?

有帮助吗?

解决方案

getTheDictionary returns an autoreleased object, which means that the object is valid in the calling method, but not owned by the caller. Therefore the calling method must not release or autorelease that object.

It will be released when the current autorelease pool ends, e.g. when program control returns to the main event loop.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top