As @bburn suggested, this is a symbol collision. There is a BSD socket() function which GCDAsyncSocket uses. If you look at the EXC_BAD_ACCESS in the debugger, you'll see that it's actually failing when attempting to call socket() because it's resolving the function address to that of your variable.
Aside from renaming the variable, you can do a couple of things (I think it would be a good idea to do one of these things even if you also rename the variable):
- Declare the variable to be static. This will confine the scope of your variable to that file so that it cannot be accidentally resolved by GCDAsyncSocket. It would be a good idea to remove it from the global namespace anyway.
- Make it a private member variable.
To change it to a private member variable, change this:
@implementation Socketmanager
GCDAsyncSocket *socket;
to this:
@interface SocketManager()
{
GCDAsyncSocket *socket;
}
@end
@implementation SocketManager
...