3

I have a following code:

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

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

Terminal shows:

[Session started at 2011-03-28 11:09:46 +0200.]
2011-03-28 11:09:51.008 Finance[35111:207] 0

2011-03-28 11:09:51.010 Finance[35111:207] 2

As I know, retainCount should be equal to 1, not 2.

Sylvain Defresne
  • 38,469
  • 11
  • 70
  • 82
  • "As I know, retainCount should be equal to 1, not 2." Are you an author of `BNPieChat`? – hoha Mar 28 '11 at 09:16
  • possible duplicate of [When to use -retainCount ?](http://stackoverflow.com/questions/4636146/when-to-use-retaincount) – Sylvain Defresne Mar 28 '11 at 09:17
  • 3
    Do not use `-retainCount`, even for debug it is not accurate. See this answer : http://stackoverflow.com/questions/4636146/when-to-use-retaincount/4636477#4636477 – Sylvain Defresne Mar 28 '11 at 09:17
  • Does BNPieChart create another object, passing a reference to itself which then gets retained? – AlexJReid Mar 28 '11 at 09:18

5 Answers5

6

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 = )

unexpectedvalue
  • 6,019
  • 3
  • 35
  • 62
  • 1
    ... also demonstrating that you should **always** think of retain counts in terms of deltas. You only increase/decrease the retain count, never look at the absolute value. As long as your increase/decrease operations are in balance, your code will not leak. – bbum Mar 29 '11 at 17:44
3

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];
klefevre
  • 8,134
  • 7
  • 37
  • 68
1

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];
Mihir Mehta
  • 13,424
  • 2
  • 59
  • 85
  • Could you explain the difference between self.chart and chart? Thanks! – Xiao Apr 02 '11 at 13:39
  • self.chart is nothing but the method getchart in Java/C++... so internally self.chart calling method getchart and if property of the chart is "retain" at the the time of assignment the get methord will alloc memory ,increase retain count and returns chart if chart is nil... where if you write simply chart you simply referring class variable chart – Mihir Mehta Apr 03 '11 at 05:35
0

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.

hooleyhoop
  • 9,077
  • 5
  • 35
  • 58
0

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.

champak256
  • 115
  • 1
  • 10