Welcome, Guest. Please login or register.
Did you miss your activation email?
July 29, 2016, 12:49:19 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 18 - Copying Objects
| | | |-+ release error question
Pages: [1] Go Down
Author Topic: release error question (Read 994 times)
Full Member
Posts: 175

on: September 26, 2011, 12:55:35 PM

Hi Stephen,

Just some practice with retain counts.  Just before [pool drain] I sent the release message to first
[first release] which brought the retain count to 0. In the output below does the error mean that the pointer is no longer allocated and so it can not be released.  Taking the last [first release] statement corrects this error. If so why?  It's a little mindbending because it seems that problems could arise when releasing an object to early.  Is this correct?

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface RetainQuestion : NSObject
    int someNumber;

@property int someNumber;


@implementation RetainQuestion

@synthesize someNumber;


int main (int argc, const char * argv[])

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    RetainQuestion *first = [[RetainQuestion alloc] init];

    NSMutableArray *myArr = [NSMutableArray array];
    NSLog(@"first retain count = %lx",[first retainCount]);
    [myArr  addObject: first];
     NSLog(@"first retain count = %lx",[first retainCount]);
    [first release];
     NSLog(@"first retain count = %lx",[first retainCount]);


    [first release];
    NSLog(@"first retain count = %lx",[first retainCount]);

    [first release]; // ANALYZE ERROR: Incorrect decrement of the reference count of an object that is not
                           // is not owned at this point by the caller
    [pool drain];
    return 0;

2011-09-26 15:53:09.277 RetainQuestion[1067:707] first retain count = 1
2011-09-26 15:53:09.279 RetainQuestion[1067:707] first retain count = 2
2011-09-26 15:53:09.279 RetainQuestion[1067:707] first retain count = 1
2011-09-26 15:53:09.280 RetainQuestion[1067:707] first retain count = 1
RetainQuestion(1067,0x7fff755dc960) malloc: *** error for object 0x100113e60: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Any help is appreciated
Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.