Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autorelease question



Hi, Daniel-

You are right. I think you did a great job of troubleshooting the problem.

In your first branch, you alloc'd and init'd your dictionary which gives you a retained object.

in your second branch, you used what is called a "convenience method" to get your dictionary which comes to you already autoreleased.

Read about it here:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html

You seem to have a good grasp of what is happening. Reading the above will give you the background of why.

On Mar 13, 2008, at 9:19 AM, Daniel Luis dos Santos wrote:

Hello,

I have the following code :

if (![fileMgr fileExistsAtPath: indexFilePath]) {
NSMutableDictionary *index = [[NSMutableDictionary alloc] init];
[index setObject: [NSNumber numberWithLong: DEFAULTS_BLOCKSIZE] forKey: INDEXKEY_BLOCKSIZE];
[index setObject: [NSNumber numberWithLong: DEFAULTS_BLOCKSPERPAGE] forKey: INDEXKEY_BLOCKSPERPAGE];
_blockSize = DEFAULTS_BLOCKSIZE;
_blocksPerPage = DEFAULTS_BLOCKSPERPAGE;
if (![index writeToFile: indexFilePath atomically: YES]) {
_dirPath = nil;
NSLog(LOGMSG_INDEXFILENOTWRITABLE);
}
[index release];
} else {
NSDictionary *index = [NSDictionary dictionaryWithContentsOfFile: indexFilePath];
if (nil == [index objectForKey: INDEXKEY_BLOCKSIZE]) {
NSLog(LOGMSG_INDEXFILEOUTOFFORMAT);
_dirPath = nil;
} else
_blockSize = [(NSNumber*)[index objectForKey: INDEXKEY_BLOCKSIZE] longValue];
if (nil == [index objectForKey: INDEXKEY_BLOCKSPERPAGE]) {
NSLog(LOGMSG_INDEXFILEOUTOFFORMAT);
_dirPath = nil;
} else
_blocksPerPage = [(NSNumber*)[index objectForKey: INDEXKEY_BLOCKSPERPAGE] longValue];
[index release];
}


the [index release] on the second branch gives me the following output on the debugger window :

TestRunnrer(1661) malloc: *** error for object 0x112a50: double free
*** set a breakpoint in malloc_error_break to debug

in the enclosing scope I have set up an autorelease pool. What I am suspecting is that when I init the dictionary with a file it is autoreleased, and then the error when I do the release. I this the case ? If so where do I know when some object is created autoreleased or not ? In the NSDictionary's reference page there is nothing mentioned.

Many thanks
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Autorelease question (From: Daniel Luis dos Santos <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.