Do I have a leak with this statement?

Statement:

//Pass the copy onto the child controller
self.childController.theFoodFacilityCopy = [self.theFoodFacility copy];

My property is set to:

@property (nonatomic, retain) FoodFacility *theFoodFacilityCopy;

I think I have a leak because it copystores the value, and then my point syntax property also stores the value. It was saved twice.

What is the correct way to write the above statement?

+3
source share
4 answers

Yes, you have a leak there.

SomeClass *someObj = [self.theFoodFacility copy];
self.childController.theFoodFacilityCopy = someObj;
[someObj release];

This reflects the recommended approach for initializing an object:

SomeClass *someObj = [[SomeClass alloc] init];
self.someProperty = someObj;
[someObj release];

In both cases, the first line returns objects with a hold value of 1, and after that you process it identically.

+9
source

, . , , .

+6

You're right. The cleanest way is something like

id temp = [self.theFoodFacitlity copy];
self.childController.theFoodFacilityCopy = temp;
[temp release]

You want to read the apple memory management site until these rules become second nature.

0
source

What is the advantage of this, but just setting the property to copy?

@property (nonatomic, copy) FoodFacility *theFoodFacilityCopy;
0
source

Source: https://habr.com/ru/post/1703055/


All Articles