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: malloc/free problems within a thread



Perhaps a code snippet would clear up the sequence of events

// in the init method for a long lived class, I initialize a mutable array

NSMutableArray		*holdingBin		= [[NSMutableArray alloc] init];

// Then I create a repeating timer
[NSTimer scheduledTimerWithTimeInterval: 5
				target: self
				selector: @selector(performDirectoryTasksWithTimer:)
				userInfo: nil
				repeats: YES];

return self

// When the timer fires, it calls performDirectoryTasksWithTimer which is defined like so:
- (void) performDirectoryTasksWithTimer:(NSTimer *) inTimer
{
[NSThread detachNewThreadSelector: @selector(scanDirectoriesInThread:)
toTarget: self
}


// I create the thread because the scanning is pretty time consuming (12 minutes on average) and I want the machine to be usable during this time
// Here's the scanDirectoriesInThread definition (where I allocate an autorelease pool)


- (void) scanDirectoriesInThread:(NSString *) inString
{
	// allocate a new autorelease pool
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	// map the directories
	[self initDirectories];
	
	// release the pool
	[pool release];
}

// Which in turn calls the method that actually performs the scan
- (void) initDirectories
{
// The malloc/free happens in here and the data in the malloc'd pointer is packed into an NSData and put in "holdingBin" allocated in the init routine
[ holdingBin addObject: [NSData dataWithBytes: ptr length: len]];


	// When I try to free pointers in here, I get those errors
}

See any obvious allocation related gotchas here?

Thanks

Ken





On Jul 29, 2006, at 6:14 PM, stephen joseph butler wrote:

2006/7/29, Ken Tozier <email@hidden>:
On Jul 29, 2006, at 4:47 PM, Ken Tozier wrote:
> I have a process that spawns threads and inside these threads I
> malloc/free some memory but I'm getting either "xxx has exited due
> to signal 11 (SIGSEGV)" or "sigbus 10" errors. I know from past
> experience that these most likely indicate a non-retained object,
> problem is though that I checked very carefully and everything that
> I want to keep around is definitely retained. Could the fact that
> some objects were created inside a thread's NSAutorelease alloc/
> release override the retain commands?

I'm confused as to what malloc/free has to do with NSAutoreleasePool. Nothing returned by malloc should ever end up in the autorelease pool. In fact, nothing returned from malloc should ever be used as an Objective-C object. All Objective-C objects come from either alloc/init (which are not autoreleased, unless you send the autorelease message) or a convience message (which are autoreleased).

It seems like the NSAutorelease pool destroys all objects created
within it regardless of whether they have a retain or not. That's the
only thing I can think of. Is that what it does?

No. It's a delayed release. It says "call release on this object sometime after my function ends."

Since you mentioned multithreading, I should point out that if you're
going to autorelease objects inside a new thread then you need to
create your own autorelease pools. See
http://developer.apple.com/documentation/Cocoa/Conceptual/ Multithreading/articles/CocoaDetaching.html#//apple_ref/doc/uid/ 20000738-97249



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >malloc/free problems within a thread (From: Ken Tozier <email@hidden>)
 >Re: malloc/free problems within a thread (From: Ken Tozier <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.