Re: NSLock and AutoreleasePool question
Re: NSLock and AutoreleasePool question
- Subject: Re: NSLock and AutoreleasePool question
- From: "stephen joseph butler" <email@hidden>
- Date: Wed, 16 Jan 2008 00:53:41 -0600
On Jan 16, 2008 12:05 AM, Development <email@hidden> wrote:
> Ok, I read up on the NSLock and becoming thread safe. My application
> is designed to download items and it does this in a loop. Each item
> may have several parts so 1 file may consist of a dozen ore more
> requests to the server. It also requires I use a number of things that
> my reading has told me are not thread safe. Particularly,
> NSMutableData and NSMutableString. Now, if I understand what I was
> reading and well, I'm not sure I did... When I alter mutable data I
> should put a lock around it correct?
>
> [aLock lock];
>
> *alter mutableData*
> [aLock unlock];
>
> Is this the correct idea?
Yes, but only if you're sharing the mutableData between threads. That
is, locks are needed when two threads might access the same variable
at the same time. If your mutableData is 100% guaranteed to only be
used by one thread at any given time (for example, it's local to your
function) then you can do without the locks.
Note: don't generalize this to AppKit. The principal still applies,
but it's tricky since AppKit itself does stuff on other threads. So
you should only use AppKit objects on the main thread unless you
really, really know what you're doing.
> Also in each loop I have an NSAutoreleasePool because if I don't this
> app will chew all the memory available in seconds. As well as the auto
> release pool around the method that is called with
> detachNewThreadSelector
> This is the problem. I have to have the pools to reclaim memory don't
> I? But I keep having crashes and the line I'm crashing at is always a
> [autoReleasePool release] call. It may be the one inside the loop, or
> the function that was originally detached, but it's always one or the
> other. This means I am either not using locks correctly, or the
> autorelease pool or both. I have tried locking the thread around the
> autoreleasepool release but that did not help. I have also tried the
> @synchronize() directive but I may not be using it correctly. I've
> tried synchronizing both the various bits of mutable data and the pool
> itself before releasing it... Can some one help shed a little light on
> this? I can provide code segments if you need to see them but they are
> too large to include here.
As long as you both (a) create the autorelease pool and (b) release
the autorelease pool on the SAME THREAD then you're fine. My guess is
that you're getting crashes for the same reason most people get
crashes: you're over-releasing an object that's in the pool. It's
probably not a thread specific issue, but a general memory management
one.
_______________________________________________
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:
This email sent to email@hidden