objective c - Why retain count is bigger, than I expect (using addSubview) -


why counter variable equals 3, not 2?

@interface scoreview : uiimageview ...   - (id)initwithframe:(cgrect)frame  {        if (!(self = [super initwithframe:frame]))          return self;      _scorelabel = [[uilabel alloc] initwithframe:cgrectmake(0,0, 10, 10)];     [self addsubview:_scorelabel];      int counter = [[[self subviews] objectatindex:0] retaincount]; // why 3?     return self; } 

-retaincount not reliable.

important: method typically of no value in debugging memory management issues. because number of framework objects may have retained object in order hold references it, while @ same time autorelease pools may holding number of deferred releases on object, unlikely can useful information method.

in case, particular reason because -subview causes subviews retained once, copying value of .layer.sublayers new array*:

uilabel* label = [[uilabel alloc] initwithframe:cgrectmake(0, 0, 10, 10)]; nslog(@"%d", [label retaincount]);  // 1 [someview addsubview:label]; nslog(@"%d", [label retaincount]);  // 2 [someview subviews]; nslog(@"%d", [label retaincount]);  // 3 

there no need worry it, array autoreleased , retaincount drop 2 later. need ensure net retain count cause current function consistent ownership status.


*: particular implementation of .subviews is:

-(nsarray*)subviews {             // irrelevant ... snipped          nsarray* sublayers = [_layer.sublayers copy];         int count = [sublayers count];              // irrelevant ... snipped              res = cfarraycreatemutable(null, count, &kcftypearraycallbacks);             (int = 0; < count; ++ i) {                 uiview* view = _uiview([sublayers objectatindex:i]);                 if (view)                     cfarrayappendvalue(res, view);                     // ^-- causes -retain each subview.             }              // irrelevant ... snipped  } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -