Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Fixed retain cycle issue causing EXC_BAD_ACCESS #105

Merged
merged 1 commit into from
Jul 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SocketIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef enum {

// heartbeat
NSTimeInterval _heartbeatTimeout;
NSTimer *_timeout;
dispatch_source_t _timeout;

NSMutableArray *_queue;

Expand Down
46 changes: 33 additions & 13 deletions SocketIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ - (void) removeAcknowledgeForKey:(NSString *)key

- (void) onTimeout
{
if (_timeout) {
dispatch_source_cancel(_timeout);
_timeout = NULL;
}

DEBUGLOG(@"Timed out waiting for heartbeat.");
[self onDisconnect:[NSError errorWithDomain:SocketIOError
code:SocketIOHeartbeatTimeout
Expand All @@ -368,16 +373,29 @@ - (void) onTimeout
- (void) setTimeout
{
DEBUGLOG(@"start/reset timeout");
if (_timeout != nil) {
[_timeout invalidate];
_timeout = nil;
if (_timeout) {
dispatch_source_cancel(_timeout);
_timeout = NULL;
}

_timeout = [NSTimer scheduledTimerWithTimeInterval:_heartbeatTimeout
target:self
selector:@selector(onTimeout)
userInfo:nil
repeats:NO];
_timeout = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0,
0,
dispatch_get_main_queue());

dispatch_source_set_timer(_timeout,
dispatch_time(DISPATCH_TIME_NOW, _heartbeatTimeout * NSEC_PER_SEC),
0,
0);

__weak SocketIO *weakSelf = self;

dispatch_source_set_event_handler(_timeout, ^{
[weakSelf onTimeout];
});

dispatch_resume(_timeout);

}


Expand Down Expand Up @@ -556,9 +574,9 @@ - (void) onDisconnect:(NSError *)error
[_queue removeAllObjects];

// Kill the heartbeat timer
if (_timeout != nil) {
[_timeout invalidate];
_timeout = nil;
if (_timeout) {
dispatch_source_cancel(_timeout);
_timeout = NULL;
}

// Disconnect the websocket, just in case
Expand Down Expand Up @@ -768,8 +786,10 @@ - (void) dealloc

_transport = nil;

[_timeout invalidate];
_timeout = nil;
if (_timeout) {
dispatch_source_cancel(_timeout);
_timeout = NULL;
}

_queue = nil;
_acks = nil;
Expand Down