EXC_BAD_ACCESS on socket create

152 views
Skip to first unread message

Lassic

unread,
Jul 30, 2012, 2:35:26 PM7/30/12
to cocoaht...@googlegroups.com
Hi,
I know this is not directly related to CocoaHTTPServer, and maybe not even GCDAsyncSocket, but I'm 
really hoping someone can point me in the right direction.
I'm integrating this server in a non-arc project, and followed the steps required to compile this project's files and dependency files as ARC.
(I might have missed something, but made sure of correct LLVM compiler and -fobjc-arc flags).

The debugger breaks with EXC_BAD_ACCESS on this line:

socket4FD = createSocket(AF_INET, interface4);

but I can actually set the last breakpoint inside that block before a crash on:
int socketFD = socket(domain, SOCK_STREAM, 0); // <-- crash

It always crashes in the same place, the stack doesn't offer any help, enabling zombies or allocations doesn't help:

GCDAsyncSocket: Creating IPv4 socket
(lldb) bt
* thread #1: tid = 0x1c03, MyApp, stop reason = EXC_BAD_ACCESS (code=2, address=0x947e28)
frame #0: MyApp
frame #1: 0x003be484 MyApp `__47-[GCDAsyncSocket acceptOnInterface:port:error:]_block_invoke_0229 + 1276 at GCDAsyncSocket.m:1585
frame #2: 0x33b9a796 libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 22
frame #3: 0x33b9a60a libdispatch.dylib`dispatch_barrier_sync_f$VARIANT$up + 62


I'm starting the server like so:

-(id)init:(NSString *) docRootPath { 

self = [super init];

[DDLog addLogger:[DDTTYLogger sharedInstance]];

// create http server

httpServer = [[HTTPServer alloc] init];

// [httpServer setType:@"_http._tcp."]; // don't want bonjour at this point 

[httpServer setPort:49494]; // I also tried with dynamic port (comment out this line)

DDLogInfo(@"Setting document root: %@", docRootPath); // this prints a good string

[httpServer setDocumentRoot:docRootPath]; 

// Start the server (and check for problems)
NSError *error = nil;
if([httpServer start:&error])
{
DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
}
else
{
DDLogError(@"Error starting HTTP Server: %@", error);
}

return self;
}


Lassic

unread,
Jul 30, 2012, 2:42:59 PM7/30/12
to cocoaht...@googlegroups.com
Forgot to mention, httpServer is declared as in the iPhone sample:

@class HTTPServer;

@interface iPhoneHTTPServerAppDelegate : NSObject <UIApplicationDelegate> {

     HTTPServer *httpServer;

Lassic

unread,
Jul 30, 2012, 4:28:26 PM7/30/12
to cocoaht...@googlegroups.com
Wow, finally figured it out, and indeed it isn't related to either libraries, so you can remove this thread.
To anyone interested in a really freaky bug read on:
It turns out a different programmer on my team defined a static/global object in some XSocket.mm file:
XSocket *socket;

I figured this out since gdb said that:
gdb> p socket
$1  = <class XScoket *> 0x...... (or something similar, it's from my head)

I was expecting it to be the same as other functions in the socket.h header, but it was pointing to one of our classes!
So I tracked that symbol down and found that awful declaration...
After removing that it just worked. Sometimes debugging is an adventure, sorry for the noise.

Lassic.



On Monday, July 30, 2012 9:35:26 PM UTC+3, Lassic wrote:

On Monday, July 30, 2012 9:35:26 PM UTC+3, Lassic wrote:
Reply all
Reply to author
Forward
0 new messages