3

How can i catch sigpipe in iphone/objective-c?

thanks

Shay
  • 2,595
  • 3
  • 25
  • 35

5 Answers5

4

One important fact for testing the SigPipeHandler:

For me it did not work when the debugger was atached. So when running an app directly from XCode the Handler is not called. Meanwhile, on a device without the debugger attached the handler works as expected.

Display name
  • 2,697
  • 2
  • 31
  • 49
  • thank you very much for posting this answer! I observed this same behavior of signals not being handled properly when debugging but being handled fine when not debugging... Weird! Anyways, this ultimately helped me realize that my app was in fact handling SIGPIPE but my real problem was that after it was handled an std:exception was thrown but not caught later in my objective c code which used `@try` `@catch` and not the C `try` `catch` blocks. – Sam Jan 16 '12 at 18:14
  • My sigpipe handler didn't seem to be called by the iOS simulator either. Maybe it was just that I was attached and debugging in xCode, but i thought it might just be the simulator. – John Bowers Feb 06 '15 at 17:34
3

Use old good POSIX code:

#include <signal.h>

void SigPipeHandler(int s);

void SigPipeHandler(int s)
{
    // do your handling
}

Init in some place (main.m?) with

signal(SIGPIPE, SigPipeHandler);
Farcaller
  • 3,070
  • 1
  • 27
  • 42
1

Try setting SO_NOSIGPIPE as documented here: How to prevent SIGPIPEs (or handle them properly)

Community
  • 1
  • 1
Simo Salminen
  • 2,306
  • 2
  • 14
  • 11
1

The first answer doesn't work. Also I'm trying to use solution described in reference of second post:

int main(int argc, char *argv[ ]) {
   struct sigaction mySigAction;
   mySigAction.sa_handler = SIG_IGN;
   sigemptyset(&mySigAction.sa_mask);
   sigaction(SIGPIPE, &mySigAction, NULL);
   ...
}

but this code doesn't work too. Anybody know solution of this problem?

iruirc
  • 143
  • 1
  • 6
1

The first answer works fine. but you should put every thing in the main.mm file.

And in static class(Singleton) , it also works.

#import <UIKit/UIKit.h>
#import <sys/signal.h>
#if TARGET_IPHONE_SIMULATOR
#endif

void SigPipeHandler(int s)
{
    NSLog(@"We Got a Pipe Single :%d____________",s);
}

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    signal(SIGPIPE, SigPipeHandler);
    int retVal = UIApplicationMain(argc, argv, nil, @"FullAppDelegate");
    [pool release];
    return retVal;
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Joe Qian
  • 493
  • 6
  • 13