title: Getting EXC_BAD_ACCESS for recursive function with return type - Objective-C
body: <p>I have following scenario:</p>
  
  <p>5 UIWebViews - 1 for question and other 4 are for options.
  Each load html string retrieved from local database. But before loading that html string, I'm customizing it, in order to have custom css, math formula,applying base64 encoded images, etc. as follows :</p>
  
  <pre><code>[self processText:queText forWebView:wvQue];
  [self processText:optText1 forWebView:wvOpt1];
  [self processText:optText2 forWebView:wvOpt2];
  [self processText:optText3 forWebView:wvOpt3];
  [self processText:optText4 forWebView:wvOpt4];
  
  -(void)processText:(NSString*)htmlString forWebView:(UIWebView*)webView withOptionButton:(UIButton*)optionButton
  {
      NSLog(@"Processing....");
  
      webView.delegate = self;
  
      if(htmlString.length == 0)
      {
          webView.hidden = YES;
          optionButton.hidden = YES;
          [webView loadHTMLString:@"" baseURL:nil]; //dummy
      }
      else
      {
          webView.hidden = NO;
          optionButton.hidden = NO;
  
          webView.scrollView.directionalLockEnabled = YES;
          webView.scrollView.showsVerticalScrollIndicator = NO;
          webView.scrollView.showsHorizontalScrollIndicator = NO;
  
          htmlString = [self applyImageIfExist:htmlString];
  
          [webView loadHTMLString:[self customizeString:htmlString forWebView:webView] baseURL:nil];
      }
  }
  </code></pre>
  
  <p>In above code, applyImageIfExist method is recursive, where sometimes getting EXC_BAD_ACCESS.</p>
  
  <pre><code>-(NSString*)applyImageIfExist:(NSString*)aString
      {
          if([aString rangeOfString:@"&lt;img alt"].location != NSNotFound)
          {
              NSString *tempStr = [NSString stringWithString:aString];
  
              NSRange tagStart = [tempStr rangeOfString:@"&lt;img alt"]; //(NSRange) tagStart = location=4, length=8
  
              NSRange tagEnd = [[tempStr substringFromIndex:tagStart.location] rangeOfString:@"/&gt;"]; //(NSRange) tagEnd = location=57, length=2
  
              NSString *imgTag = [tempStr substringWithRange:NSMakeRange(tagStart.location, tagEnd.location)];
  
              if(imgTag != nil)
                  imgTag = [imgTag stringByAppendingString:@"/&gt;"];    //&lt;img alt="" src="IMG-725bd4a7f35ecdc6-20160405191358.png"
  
              for (NSString *iName in [allQuestionImages allKeys])
              {
                  if([imgTag rangeOfString:iName].location != NSNotFound)
                  {
                      NSString *dataTag = [CommonUtility setStringIfNotNull:[[allQuestionImages valueForKey:iName] valueForKey:@"DataUri"]];
  
                      tempStr = [tempStr stringByReplacingOccurrencesOfString:imgTag withString:dataTag];
  
                      break;
                  }
              }
  
              NSLog(@"Applying image : Recursion ...");
              return [self applyImageIfExist:tempStr];
          }
          else
          {
              NSLog(@"Returning ...");
              return aString;
          }
      }
  </code></pre>
  
  <p>Anyone knows why this is happening. Is this threading issue ? Should I use @synchronized block ? ... ???</p>
  
  <p>Thanks !</p>