5

Apple developer sample of Reachability cause Memory Leak when turning off wifi during play offline music (using this code) in my app and it got stuck line I mentioned bottom, It may cause by that Player code because log says [__NSCFString reachabilityDidChange:]: unrecognized selector

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];

in this method:

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [CDVReachability class]], @"info was wrong class in ReachabilityCallback");

    CDVReachability* noteObject = (__bridge CDVReachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotificationString object: noteObject];
}

Here is log:

2015-04-12 19:52:03.416 HelloCordova[4094:1250289] -[__NSCFString reachabilityDidChange:]: unrecognized selector sent to instance 0x174232820

And reachabilityDidChange is in this file

#import "ReachabilityManager.h"
#import "Reachability.h"

@interface ReachabilityManager ()
@property NetworkStatus previousReachability;
@end

@implementation ReachabilityManager

- (id) init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(reachabilityDidChange:)
                                                name:kReachabilityChangedNotification
                                                object:nil];
        self.previousReachability = -1;
    }
    return self;
}

- (void) reachabilityDidChange:(NSNotification *)notification {
    NSLog(@"reachabilityDidChange!");

    CDVReachability * r = [notification object];
    NetworkStatus ns = [r currentReachabilityStatus];

    if (self.delegate && [self.delegate respondsToSelector:@selector(reachabilityDidChangeFrom:to:)]) {
        [self.delegate reachabilityDidChangeFrom:self.previousReachability to:ns];
    }
    self.previousReachability = ns;
}

@end

I don't know is that correct but I try this:

dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    });}

and even this:

// We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
    // in case someon uses the Reachability object in a different thread.
    @autoreleasepool {
        Reachability* noteObject = (__bridge Reachability*)info;
        // Post a notification to notify the client that the network reachability changed.
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    }
Developia
  • 3,928
  • 1
  • 28
  • 43

0 Answers0