Central Dispatch and memory management

Discussion in 'Mac Programming' started by DutchMark, Jul 16, 2010.

  1. DutchMark macrumors newbie

    Joined:
    Jul 16, 2010
    #1
    I love the ease with which I can do concurrency with Central Dispatch, but today I hit what looks like a strange snag. My program crashes when one of the job terminates. The place is not always the same, but it seems to go wrong while auto releasing memory. My threads do not share any memory whatsoever, so it's hard to see how it could interfere.

    I do allocate a NSMutableDictionary in each thread like this:

    mValuesTable = [ [ NSMutableDictionary dictionaryWithCapacity:32 ] retain ];

    and free it like this in the destructor:

    [ mValuesTable release ];

    In the beginning of the application I do set an auto release pool as follows:

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

    Is it possible that NSAutoreleasePool is not thread-safe? I find it hard to imagine, but wanted to make sure...

    Any help is appreciated.

    Mark
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You need to post more code. But generally if the crash is not in the same place, there's a good chance you're auto-releasing an object that has already deallocated.
     
  3. DutchMark thread starter macrumors newbie

    Joined:
    Jul 16, 2010
    #3
    You need to post more code. But generally if the crash is not in the same place, there's a good chance you're auto-releasing an object that has already deallocated.

    I'll try to narrow it down to code that is short enough to post. It may help me determine what's happening exactly anyway.

    But if I were auto-releasing an already deallocated object, then it should also give an error when running in a single thread, yet it doesn't.

    If it's accessing a data-structure that's not thread-safe, then it's quite normal not to crash in the same place, as the timing will be quite random when exactly two threads will collide, accessing the same data in a way to make it crash.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Not necessarily, it depends on how your autorelease pool is setup. If you're running it all in a single thread and only have a single autorelease pool, you won't see autorelease errors until the pool empties, which wouldn't happen until the program terminates, but only if you remember to release/drain the pool.
     

Share This Page