1

I have tried
(1) cleaning build folder,
(2) deleting app from the simulator and from my iphone,
(3) rebooting,
(4) and enabling NSZombieEnabled but it does not appear to be doing anything.
(5) using debugger lldb and gdb
(6) and gaurd malloc

I have xcode 4.3.2. This bug occurs on both the simulator and my iphone. (on the simulator it says EXC_BAD_INSTRUCTION code=i386 subcode=0x0 and on the device it says EXC_BREAKPOINT (SIGTRAP).
Here are two crashes logs:

http://pastie.org/3941419

http://pastie.org/3941427

In the log it prints:

Trace/BPT trap: 5
Segmentation fault: 11

As you can see in the crashes, there is no traceback to any of my ViewControllers. I did a lot of googling before asking (link), but a lot of other people(link) with this error have a traceback to thier app. What are common causes of this app and where should I start looking for a bug? There is not one thing I can do to reproduce, just run a piece of my code like 10 times and it will crash at least once. Thanks!

EDIT: heres some code

outputPipe = [[NSPipe alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataOut:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];

[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];

I do release outputPipe later in my code.

EDIT 2: changing [NSPipe pipe]; to [[NSPipe alloc] init]; did not fix it.

Code to switch between observers:

 buttondone.title=@"Done";
    //dlog(@"last output notification inbound");
    stdoutisdone=YES;

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadCompletionNotification object:[outputPipe fileHandleForReading]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataOut:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];

    [[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
Community
  • 1
  • 1
apple16
  • 1,137
  • 10
  • 13
  • 1
    I would guess that `_performFileHandleSource` relates to one of the "InBackgroundAndNotify" methods of `NSFileHandle`. Are you using those? Perhaps you're over-releasing the file handle object. Alternatively, you may have registered an observer of the notifications emitted by those methods but allowed the observer to be deallocated without unregistering it. – Ken Thomases May 20 '12 at 20:55
  • I do not use NSFileHandle in any relevant code (and I never release it) but I do use `[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[errorPipe fileHandleForReading]];` – apple16 May 20 '12 at 20:58
  • My bad, I do use NSFileHandle, I thought about NSFileManager when I read that... – apple16 May 20 '12 at 21:16
  • Isolated statements are not very informative. We need to see context. What holds/owns `outputPipe`? When is it released? What adds itself as the observer and when? When and under what circumstances does it remove itself as an observer? When is `-readToEndOfFileInBackgroundAndNotify` called? What is writing to it? Is this for an `NSTask`? Are you holding a reference to the `NSTask` object? When is that released? Is the crash happening just after the task exits or at some other unrelated time? – Ken Thomases May 20 '12 at 23:57
  • outputPipe is owned by viewoutputcontroller, it is released when the user leaves the view, and `alloc` when the user enters the view. I remove it as an observer when my`NSTask` finishes. I continuously call `readInBackgroundAndNotify`and when the task finishes, I call `readToEndOfFileInBackgroundAndNotify` to read the rest of the output. It seems that whenever `readToEndOfFileInBackgroundAndNotify` found data, it crashed. I changed it to a `[[outputPipe fileHandleForReading] readDataToEndOfFile];`and it didnt crash(but does lag UI). Thanks for your help. :) PS Should using the 2x notify crash? – apple16 May 21 '12 at 01:28
  • Well, I'm glad I helped you find a workaround, but we haven't figured out the reason for the crash and that UI lag doesn't sound good. You said you remove the observer when the task finished. You also said you call `readToEndOfFileInBackgroundAndNotify` when the task finishes. Those seem contradictory. – Ken Thomases May 21 '12 at 01:34
  • I removed the `readInBackgroundAndNotify` observer when the task finishes (in the `-getDataOut:` so the notifications could never overlap), added the `readToEndOfFileInBackgroundAndNotify` observer, called the `readToEndOfFileInBackgroundAndNotify` method once to get the rest of the output, and then removed its observer when its notification was received. – apple16 May 21 '12 at 01:40
  • Any chance the observer could have been deallocated before the background read to EOF completed? For example, maybe in response to the completion of the task? – Ken Thomases May 21 '12 at 01:56
  • No, I dont think that is the problem, I pasted my code to switch between the two observers.- Even with the `readToEndOfFileInBackgroundAndNotify` observer on the entire time( activated with the other observer so niether of the observer lines would be there) it still crashed. – apple16 May 21 '12 at 02:05
  • That code isn't relevant. The question is, after doing `-readToEndOfFileInBackgroundAndNotify`, is the `self` object guaranteed to stick around until that read has completed? Or might it be deallocated before then? – Ken Thomases May 21 '12 at 02:08
  • It is guaranteed to stick around. There are no other methods called. – apple16 May 21 '12 at 02:29

1 Answers1

0

I solved it by removing the call to the second NSFileHandle method. So, instead of this:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadCompletionNotification object:[outputPipe fileHandleForReading]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataOut:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];

[[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];

I use this:

NSString * output=[[outputPipe fileHandleForReading] readToEndOfFile];

It does produce a little UI lag which can be reduced by called readInBackgroundAndNotify once or twice after the task has finished. If anyone has a better solution I will mark yours as the answer.

apple16
  • 1,137
  • 10
  • 13