Objective c, Memory management of instance members

后端 未结 6 800
南方客
南方客 2020-12-02 00:58

I am confuzed by the memory management of instance members. I have a class with a ivar:

DetailedResultsTableViewController *detailedResultsTableViewControlle         


        
6条回答
  •  悲哀的现实
    2020-12-02 01:33

    When you have a retained property it increments the retain count on any self.myProperty =

    Alloc also increments the retain count. So in your case the retain count is 2.

    There a couple approaches.

    1. You could include an autorelease in your init alloc statement
    2. Create a temporary variable while you set up your instance then when you are done set your property to it and release the temp.
    3. Drop the self. for this assignment. The catch here is if you have a custom setMyVariable: function then it will not get called without the self.
    4. Use ARC and you don't have to worry about any of this.

提交回复
热议问题