I have inherited application developed on 10.6 and I want to migrate on 10.7. I would like to comply with Automatic Reference Counting and I started it. Conversion assistant is sending me and error message: '[rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately' and points to following method:
+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
MyClass *newObject = [[MyClass new] autorelease];
[newObject deserializeAttributesFromNode:cur];
[newObject deserializeElementsFromNode:cur];
return newObject;
}
This would be pretty much normal style (except for ugly 'new' message)开发者_开发百科 in old retain/release environment, however, ARC environment does not allow this. It doesn't seem to me very good solution, but should I create poll with new directive, like this? Is this correct at all?
+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
MyClass *newObject;
@autorelease
{
newObject = [MyClass new];
[newObject deserializeAttributesFromNode:cur];
[newObject deserializeElementsFromNode:cur];
}
return newObject;
}
Wouldn't that release 'newObject' before return?
The @autorelease
block just creates a new autorelease pool around that section of code. It doesn't do anything for the actual memory management of the code inside that block.
I think the problem the compiler is trying to point out to you is that you're returning an autoreleased object from a method that doesn't follow the naming convention for a method returning an auto-released object.
精彩评论