0

I'm using the Reachability class provided here by Apple in my iOS project. I call its currentReachabilityStatus method always before trying to call my own REST web services:

- (NetworkStatus)currentReachabilityStatus
{
   NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
   NetworkStatus returnValue = NotReachable;
   SCNetworkReachabilityFlags flags;

   if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
   {
       if (_alwaysReturnLocalWiFiStatus)
       {
           returnValue = [self localWiFiStatusForFlags:flags];
       }
       else
       {
           returnValue = [self networkStatusForFlags:flags];
       }
   }

   return returnValue;

}

In turn, I call this method from a custom class I made for convenience:

+ (BOOL)checkNetStatus
{
   Reachability *reach = [Reachability reachabilityForInternetConnection];
   NetworkStatus status = [reach currentReachabilityStatus];
   return [self boolFromStatus:status];
}

I'm performing some tests in an iPhone, enabling the flight mode in its Settings, and then when the app is back to foreground, that method is called several times (my app retries to call the web services if no reachability until they become reachable) and finally I get an EXC_BAD_ACCESS exception at line if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)), and the app crashes.

I don't understand exactly why, because currentReachabilityStatus is called several times before I get the exception and the crash, could it be because it is being called a lot of times and too fast? How could I solve this?

I need help, thanks in advance.

EDIT: whenever I'm going to call of my RESTful services, I do something like this:

- (void)callWebService
{
   if ([MyReachabilityManager checkNetStatus]) {

      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

      MyServiceWrapper *requestService = [[MyServiceWrapper alloc] initWithServiceUrl];
      [requestService queryService];
   }
   else {
      [self keepRequestingMyService]; // this calls this method again until a timeout
   }
}
AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

0

Something similar happened to me, one of my application crashed while switching to Airplane mode and to Wifi frequently (Happened if the Wi-Fi is having low signal strength).

In my case the crash is caused by the NSAssert, the _reachabilityRef was null, so the assert condition was failing:

NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");

I just removed that and re-wrote that method by adding a extra null check for _reachabilityRef

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thanks. In my case, the `_reachabilityRef` is not null at the point I get the error... I'd be something related to memory management, taking into account that I'm findind this when calling the method a lot of times in a row because of the lack of reachability, but I don't know how to handle it... – AppsDev Nov 07 '15 at 18:16
  • Hi @Midhun MP can you please add your method. I am also facing the same issue. I tried but it didn't work. Please add this condition in your answer. – Mihir Oza Jul 13 '16 at 12:03
0

I had the same: app SOMETIMES crashes when releasing reachabilityRef. Seems like it should be NULL but it is not? enter image description here

After experimenting i found if I added something in Reachability.h interface (originally it had nothing):

@interface Reachability : NSObject{
    BOOL dummy;
}

it stopped crashing, could you try that and report if it helps?

I use XCode 7.1.1

Boris Gafurov
  • 1,427
  • 16
  • 28