proper memory handling for an NSMutableArray assigned to property?

前端 未结 3 1414
余生分开走
余生分开走 2021-01-28 04:52

I have a property declared like this:

@property (nonatomic, retain) NSMutableArray *pricingLevels;

And I assign it like this:

          


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 05:44

    Yep, you have a memory leak. Assigning to self.propertyName for a retained property automatically performs a retain. You either need to release the object after assigning (slightly more efficient to just do release after assigning vs using autorelease), or you need to assign the retained object to the instance field (sans self.) not the property name, AND, for this latter case, you need to be sure that the instance field was previously nil (ie, only do the direct assignment in initialization logic).

    (And don't forget your dealloc method.)

    (This all has nothing to do with the fact that the object happens to be an NSMutableArray.)

提交回复
热议问题