0

I'm using CocoaAsyncSocket library to create a TCP socket connection. The problem I'm having is after a few library methods are called, I'm getting an error.

appDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    NSError *error = nil;
    if (![socket connectToHost:@"199.5.83.63" onPort:11005 error:&error]) 
    {
        NSLog(@"Error connecting: %@", error);
    }

    [socket readDataWithTimeout:10 tag:1];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[tekMatrixViewController alloc] initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

And here are my methods from the CocoaAsyncSocket Library:

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
    NSLog(@"RX length: %d", [data length]);
    if(msg)
    {
        NSLog(@"RX:%@",msg);
    }
    else 
    {
        NSLog(@"Fail");
    }    
}

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"error - disconnecting");
    //start reconnecting procedure here...
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"disconnected");
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"connected");
}

When I run my app in the simulator, this is what my output log spits out:

2012-06-08 13:17:30.808 tekMatrix[2793:f803] connected
2012-06-08 13:17:30.815 tekMatrix[2793:f803] RX length: 8
2012-06-08 13:17:30.816 tekMatrix[2793:f803] Fail

After that, I get an error in the AsyncSocket.m file (part of library) in this method:

- (void)scheduleDequeueRead
{
    if((theFlags & kDequeueReadScheduled) == 0)
    {
        theFlags |= kDequeueReadScheduled;
        [self performSelector:@selector(maybeDequeueRead) withObject:nil afterDelay:0 inModes:theRunLoopModes];
    }
}

Specifically, the error is on line:

[self performSelector:@selector(maybeDequeueRead) withObject:nil afterDelay:0 inModes:theRunLoopModes];

And the exception is: Thread 1: BAD_EXC_ACCESS (code 1=0, address=0xd0688b8a)

After that, the app is completely frozen in the simulator. If anybody could offer some insight as to why this is causing the app to freeze, I would really appreciate it.

Here is the library I'm using: CocoaAsyncSocket

EDIT:

After Enabling Zombie Objects and running the app in the simulator, this is spit out in the output:

2012-06-08 14:53:15.416 tekMatrix[3217:f803] *** -[__NSArrayI count]: message sent to deallocated instance 0x6d10a70

I'll have to do a little digging on this and figure out what's happening.

EDIT 2:

After a little digging using instruments, I found out the following:

An Objective-C message was sent to a deallocated object (zombie) at address: 0x6b8bb10.

EDIT 3:

Now the error reads:

Thread 1: EXC_BREAKPOINT(code=EXC_I386_BPT, subcode=0x0)

Here is a screenshot from instruments. I'm not really following how to interpret this, though. It looks like I'm sending a message to an object that has been deallocated. Is this true? If so, how do I go about figuring out where this occurs?

If the image is hard to see, here's a direct link: Instruments Screenshot

Instruments Screenshot

Skizz
  • 1,211
  • 5
  • 17
  • 28
  • can you show the code, where you create the AsyncSocket object? along with the property, if it is written to one. – vikingosegundo Jun 11 '12 at 16:04
  • AsyncObject is at the top of the didFinishLaunchingWithOptions method. I inadvertently left out that line of code when I was weeding through commented out lines. – Skizz Jun 11 '12 at 18:07
  • I don't know if this helps, but if I remove the line "[socket readDataWithTimeout:10 tag:1];" then the app doesn't freeze or generate an error. It only connects to the host and doesn't read any data. – Skizz Jun 11 '12 at 18:31
  • It doesn't look like it. Most of the code I have put together has been collectively put together between tutorials and help from SO users. Why would I need to use a property socket? – Skizz Jun 12 '12 at 13:10
  • you know, what a property is? – vikingosegundo Jun 12 '12 at 13:29
  • My understanding is that properties are used to easily take advantage of an object's accessor methods. – Skizz Jun 12 '12 at 13:46

2 Answers2

2

You are trying to access a object, that doesnt exsits anymore. Probably you are under-retaining/over-releasing it.

see this answer for a tool to find such objects: How do I set up NSZombieEnabled in Xcode 4?

Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
1

I know it's way after the fact, but I just had this same exact problem. The solution was adding the ARC flag to AsyncSocket.m, -fobjc-arc

:)

Famer Joe
  • 161
  • 1
  • 4