I am very new for objective c.
I have experiment a convenient constructor for memory leaks.
Here is my constructor
+(Myobject * ) test{
return [[self alloc] init];
}
and I test it with this code in main.m
Myobject * __weak g = [Myobject test];
NSLog(@"%@",g);
I wish the log will say (null) because pointer in constructor is died when it out of scope and arc will do remove this object out of memory since there is no strong pointer to retain it.
just only weak pointer.
But in the log it say something that an object is still alive.
In my understand now there is a strong pointer that retain this object in the constructor method.So it will be there forever.
So how can i get rid of that pointer?
Or Did i miss something?
Your
testmethod returns an autoreleased object. If there is no strong reference to the object, it will be destroyed at the end of the current autorelease pool. You can verify that withDue to ARC naming conventions the behaviour is different if you rename your method to
newTest. In that case it returnes a retained object which is released immediately.The exact semantics can be found in Automatic Reference Counting, in the sections “3.2.2. Retained return values” and “3.2.3. Unretained return values”.
Correction: As Nikolai Ruhe correctly pointed out, it is not guaranteed that the object returned by
[Myobject test]is in the autorelease pool, the compiler can remove retain/release/autorelease calls during optimization.