title: Random Error on synchronous request on Mac OS X agent
body: <p>On MacOS, we have an agent that implements ARC and that will make some
  requests to the server each 10 or 15 seconds depending on the user's
  settings and was working without any problems for almost a year, and
  just a couple of weeks ago the application crashed in one computer with
  a Bad access error, specifically on this line:</p>
  
  <pre><code>[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;responseCode error:&amp;error];
  </code></pre>
  
  <p>The Mac OS X version of the machine that is causing problems is 10.7.5, however in other computers with the same OS version is working just fine.</p>
  
  <p><strong>We are using synchronous requests since that is what we need to do the
  work, however we spent some time making the calls asynchronous but the
  problem persists.</strong></p>
  
  <p>So, after looking at other posts we added a cache policy: [request
  setCachePolicy: NSURLRequestReloadIgnoringCacheData]; to avoid any
  problem with a cache request.  The application does run better but is
  still crashing usually between 1500 - 1800 iterations (30-40 minutes),
  before it was crashing from between 15 or 20 iterations.</p>
  
  <p>By looking on another questions in stack overflow we tried fix this by using ASIHTTPRequest but again the problem happened randomly (it could crash in the iteration #2 or #123x... as well).</p>
  
  <p>Before the error appears, the request is always working properly, we are
  getting the data and able to work with it normally.</p>
  
  <p>With NSZombieEnabled option enabled we are not getting any message when
  the application crashes, try/catch block is not working for us either
  since the error is pointing to the specific line from above;</p>
  
  <pre><code>[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;responseCode error:&amp;error]
  </code></pre>
  
  <p>This is the code that we have to make the request using NSURLConnection:</p>
  
  <pre><code>   NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
     init]; [request setHTTPMethod:@"GET"]; [request setURL:[NSURL
     URLWithString:url]]; [request setTimeoutInterval: TIMEOUT];
     [request setCachePolicy: NSURLRequestReloadIgnoringCacheData];
  
      NSString *authenticationHeader = [NSString
      stringWithFormat:@"Basic %@", credentials]; [request
      addValue:authenticationHeader
      forHTTPHeaderField:@"Authorization"];
  
      NSError *error = nil; NSHTTPURLResponse *responseCode = nil;
  
      NSData *responseData = nil; responseData = [NSURLConnection
      sendSynchronousRequest:request returningResponse:&amp;responseCode
      error:&amp;error];
  
      if([responseCode statusCode] != 200){ *hasError =[NSString
      stringWithFormat: @"Error getting %@, HTTP status code %li",
      [responseCode statusCode]]; return @"";
      }
      }
      return [[NSString alloc] initWithData:responseData
      encoding:NSUTF8StringEncoding];
  </code></pre>
  
  <p>And this is the code for ASIHttpRequest:</p>
  
  <pre><code>(NSString *)getDataFromURL: (NSString *)urlString withB64Credentials:(NSString *)credentials error:(NSString **)hasError
  {
      NSString *response = @"";
      NSURL *url = [NSURL URLWithString:urlString];
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
      [request setCachePolicy:ASIDoNotReadFromCacheCachePolicy];
      [request setTimeOutSeconds:TIMEOUT];
  
      NSString *authenticationHeader = [NSString stringWithFormat:@"Basic %@", credentials];
      [request addRequestHeader:@"Authorization" value:authenticationHeader];
      [request startSynchronous];
  
      NSError *error = [request error];
      if (!error)
          response = [request responseString];
      else
          NSLog(@"%@", [error description]);
      return response;
  }
  </code></pre>
  
  <p>Any suggestion would be appreciated.</p>