NSLock and AutoreleasePool question

  • 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?

    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.

    Thanks.
  • On Jan 16, 2008 12:05 AM, Development <development...> 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.
  • On Jan 15, 2008, at 11:53 PM, stephen joseph butler wrote:

    > 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.

    Hmm. that makes sense. I'll make sure I'm not releasing the same
    object more than I should be.

    Thank you.
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today