IPhone: Is it a leak or not

Recently, someone from Qaru told me that the code below does not flow, that the property handles the save itself:

self.locationManager = [[CLLocationManager alloc] init];

in dealloc:

   self.locationManager = nil;

where in the .h file:

@property (nonatomic, retain) CLLocationManager *locationManager;

I thought it was an obvious leak, and believed that this should fix the leak:

self.locationManager = [[[CLLocationManager alloc] init] autorelease];

However, he argued that this would not work, because in his words: “you do not have the autoclassical properties of the class. The autogenerated accessory of the property defined for preservation automatically processes the preservation”

And he made me wonder if he was not mistaken or I didn’t understand memory management at all?

EDIT 1 : Is the code

self.myName=[NSSting stringWithFormat:@"%@ is correct.", @"TechZen"];

different

 self.locationManager = [[[CLLocationManager alloc] init] autorelease];

Memory Management - Wise?

, . ? , , - , . , .

+5
7

. . locationManager 2 : alloc/init, - . nil locationManager.

, 1, . , , autorelease.

+7

:

  • ( )

, CLLocationManager 2 . alloc . :

CLLocationMamnager *aLocationManager = [[CLLocationManager alloc] init];
self.locationManager = aLocationManager;
[aLocationManager release];

, , , . :

self.locationManager = [[[CLLocationManager alloc] init] autorelease];

, . ( ), , .

+4

, :

self.locationManager = [[CLLocationManager alloc] init];

, , :

self.locationManager = [[[CLLocationManager alloc] init]autorelease];

, dealloc .

, , :

locationManager = nil;

locationManager , nil, .

, , locationManager , reset :

self.locationManager = foo;

, : crash exc_bad_access, locationManager, foo:

self.locationManager = [[[CLLocationManager alloc] init]autorelease];
[locationManager release];
self.locationManager = foo;
+2

@jnic: . , , , . , , , , ahmet emrah

+1

: "", ( ), , , .

+1

... , , locationManager. , .

NSLog(@"retainCount:%d", [locationManager retainCount]);

:

[locationManager release];
0

, .

, ...

@property (nonAtomic, retain) NSString myName;

... - property, :

@property (nonAtomic, readwrite, retain, getter=getMyName,setter=setMyName) NSString myName;

@synthesize myName; , getter, :

-(void) setMyName:(NSString *) aString{
    if (!(myString==aString) { //test if a string is the same **object** as the current myString
        if (aString != nil) { // if nil we don't want to send a retain
            [aString retain]; // increment the retain count by one
        }        
        [myString release]; //decrement the retain count of the currently assigned string by one.
        myString=nil; //set the pointer to nil to ensure we don't point to the old string object
        myString=aString; //assign the newly retained object to the myString symbol     
    }
}

, , , . . , ( aString) , .

...

self.myName=[NSSting stringWithFormat:@"%@ is correct.", @"TechZen"];

:

self.myName=[[NSSting stringWithFormat:@"%@ is correct.", @"TechZen"] retain];

... , , ​​ autoreleasepool .

, - ...

[self.myName release]; 

... - dealloc, my , . , ..

[self.myName retain];

... , (, .)

, , . , . , , , setter , , .

, , , self object.

, , . . save dealloc. .

self.propertyName self, .

-2

All Articles