简体   繁体   中英

memory management warning in Iphone

I have some doubts in objective-C programming. I have function like this.

+ (NSManagedObjectContext *) newContext {
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
    NSManagedObjectContext* managedObjectContext = [[[NSManagedObjectContext alloc] init] autorelease];
    [managedObjectContext setPersistentStoreCoordinator:coordinator];
    return managedObjectContext;
}
return nil;

}

But when I run analyse on my project I am seeing something like this with warning

"Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected"

我运行分析后看到的内容

Can someone point me out so as to why Xcode is giving me a warning here. What is the correct way to return any variable?

The problem lies not within your code but within the name of your method. In Objective C, methods whose names start with init or new are assumed by the analyser to return an object with a retain count of 1. You are returning an object with a retain count of zero, therefore your method name should not contain new .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM