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

Commit

Permalink
Changed timeout timer from NSTimer to GCD timer to avoid retain cycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonholmquist committed Jun 12, 2013
1 parent 79fac8e commit 5b43476
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
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

1 comment on commit 5b43476

@pkyeck
Copy link
Owner

@pkyeck pkyeck commented on 5b43476 Nov 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you take a look at #136 ?
would be great. thanks.

Please sign in to comment.