one alloc, retainCount == 2

前端 未结 5 1698
情话喂你
情话喂你 2020-12-19 20:12

I have a following code:

NSLog(@\"%d\", [chart retainCount]);

self.chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
NSLog(@\"%d\", [chart retainC         


        
相关标签:
5条回答
  • 2020-12-19 20:29

    You chart property defined as retain or copy, so:

    self.chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
    

    +1 retain at alloc ([BNPieChart alloc])
    +1 retain at assignment (self.chart = )

    0 讨论(0)
  • 2020-12-19 20:37

    chart is probably a retained property, that's why you have 2 retainCount. That's why you can see some declaration like that :

    BNPieChart *aChart = [[BNPieChart alloc] initWithFrame:self.view.frame];
    self.chart = aChart;
    [aChart release];
    
    0 讨论(0)
  • 2020-12-19 20:38

    There are 3 possible problems I see:

    1. When you synthesized the chart property, it was synthesized with a retain attribute
    2. You have called retain in a self-implemented getter method
    3. Whoever wrote the initializer method for BNPieChart or its superclass's designated initializer had a retain in the initializer.

    Have you seen the code for BNPieChart and its non-Cocoa superclasses? If you can, try to post the initializer code.

    0 讨论(0)
  • 2020-12-19 20:40

    Due to self in the statement its retain count is 2 as property of the chart is declared as retain Remove self from the statement

    change

     self.chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
    

    to

     chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
    
    0 讨论(0)
  • 2020-12-19 20:44

    How do you know that the retainCount should be 1? Are you the author of the -setChart: method you are calling? How is it implemented? Why didn't you include it in the post?

    Nothing you have posted here is suspicious.

    0 讨论(0)
提交回复
热议问题