Crash and dispatch queue

  • I have a method that's crashing with EXC_BAD_ACCESS. I'm running a fairly lengthy, iterative process in a distpatch_async block and at the end of it saving a file. Originally, I had included the file save in the block...

    dispatch_async(queue,^{
          [myObject doSomeHorrificCrunching];
          [myObject saveDataToFile];
        });

    ...and was getting a crash in certain situations -- it seemed to be related to the size of the file (which also indicates the length/complexity of the iterative process). So, I tried pulling the file save out of the block:

    dispatch_async(queue,^{
          [myObject doSomeHorrificCrunching];
        });

    [myObject saveDataToFile];

    This also crashed (which is not surprising). So, just to check my theory that perhaps the data was still being modified when the file save method was run, I used dispatch_sync:

    dispatch_sync(queue,^{
          [myObject doSomeHorrificCrunching];
        });

    [myObject saveDataToFile];

    This didn't crash, but of course I had to enjoy the glorious beach ball.

    So, finally, I tried using a group (and going back to dispatch_async)...

    dispatch_group_async(group, queue,^{
          [myObject doSomeHorrificCrunching];
        });

    dispatch_group_notify(group, queue, ^{
        [myObject saveDataToFile];
        });

    ...but to my dismay, the crash came back.
    I'm wondering: does this last step actually prove that my crash is _not_ being caused by trying to save while the data is being modified?
    Unfortunately, I haven't been able to get any info on the state of the object when the crash happens. I have Zombies enabled, but I don't get any zombie action in the console.
    Confused...

    J.

    ------------------------------------------------------
    James B. Maxwell
    Composer/Researcher/PhD Candidate
  • James,

    We need a lot more information before we can give any help. Please strongly consider reducing your experience down to a compilable test case.

    Good luck,

    davez

    On Nov 17, 2011, at 1:49 PM, James Maxwell wrote:

    > I have a method that's crashing with EXC_BAD_ACCESS. I'm running a fairly lengthy, iterative process in a distpatch_async block and at the end of it saving a file. Originally, I had included the file save in the block...
    >
    > dispatch_async(queue,^{
    > [myObject doSomeHorrificCrunching];
    > [myObject saveDataToFile];
    > });
    >
    >
    > ...and was getting a crash in certain situations -- it seemed to be related to the size of the file (which also indicates the length/complexity of the iterative process). So, I tried pulling the file save out of the block:
    >
    > dispatch_async(queue,^{
    > [myObject doSomeHorrificCrunching];
    > });
    >
    > [myObject saveDataToFile];
    >
    > This also crashed (which is not surprising). So, just to check my theory that perhaps the data was still being modified when the file save method was run, I used dispatch_sync:
    >
    > dispatch_sync(queue,^{
    > [myObject doSomeHorrificCrunching];
    > });
    >
    > [myObject saveDataToFile];
    >
    > This didn't crash, but of course I had to enjoy the glorious beach ball.
    >
    > So, finally, I tried using a group (and going back to dispatch_async)...
    >
    > dispatch_group_async(group, queue,^{
    > [myObject doSomeHorrificCrunching];
    > });
    >
    > dispatch_group_notify(group, queue, ^{
    > [myObject saveDataToFile];
    > });
    >
    > ...but to my dismay, the crash came back.
    > I'm wondering: does this last step actually prove that my crash is _not_ being caused by trying to save while the data is being modified?
    > Unfortunately, I haven't been able to get any info on the state of the object when the crash happens. I have Zombies enabled, but I don't get any zombie action in the console.
    > Confused...
    >
    > J.
    >
    > ------------------------------------------------------
    > James B. Maxwell
    > Composer/Researcher/PhD Candidate
  • On Nov 17, 2011, at 1:49 PM, James Maxwell wrote:

    > dispatch_async(queue,^{
    > [myObject doSomeHorrificCrunching];
    > [myObject saveDataToFile];
    > });

    My first guess is the (parent) function that is calling dispatch_async, somewhere (or some other function it calls) is releasing or otherwise invalidating myObject, or something that myObject relies on, before doSomeHorrificCrunching completes.

    You didn't mention if running it without the dispatch queue would crash it - although you did say switching to dispatch_sync does stop it from crashing.

    You didn't mention if you are using the dispatch queue as a synchronizer for multiple access or not; that is, all the calls that touch myObject, are they issued from blocks on the same queue?

    I'd probably start by nslogging points where threads might end and see if you're getting events out of order.

    --Jim
  • > [myObject saveDataToFile];

    What does this method actually do?  From the sounds of it your analysis has determined that running this from a thread other than the main one is problematic.

    If so, first thing is to figure out why the thread matters at all.  If you can't make it thread safe, then perform your work asynchronously as in your first case, but wrap the call to save in a dispatch back to the main queue.
previous month november 2011 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        
Go to today