title: vanished self.delegate in arc: EXC_BAD_ACCESS
body: <p>I have a SurroundViewController (CollectionView) which shows images loaded from a webserver. If you click on the image you will be navigated to a DetailViewController (TableView) which shows additional information to the image. Both Emebded in a NavigationController (see storyboard image). </p>
  
  <p><img src="https://i.stack.imgur.com/PkBDm.png" alt="Storyboard of ViewController setup"></p>
  
  <p>My problem start when I do a refresh in the SurroundViewController, when coming back from the DetailViewController. Then it crashes with <code>EXC_BAD_ACCESS</code> on the <code>performSelector</code> line</p>
  
  <h3>WebApi.m</h3>
  
  <pre class="lang-c prettyprint-override"><code>-(void)getSurroundStream {
  
      NSString *URLString = [NSString stringWithFormat:@"%@/%@/view/%f/%f", kApiHost, kApiPath, self.sshare.coordinate.longitude, self.sshare.coordinate.latitude];
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  
      [self setAuthHeader:manager];
  
      [manager GET:URLString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [self.sshare putViData:responseObject];
  
          [self.delegate performSelector:@selector(didLoadFoo)]; // --&gt; EXC_BAD_ACCESS
  
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          [self.error vError:error message:operation.responseString url:URLString];
      }];
  }
  </code></pre>
  
  <p>I checked in the Debug Console:</p>
  
  <pre><code>2014-03-11 14:22:51.989 Foo[6923:60b] -[SurroundViewController refresh:] [Line 352] refreshing
  2014-03-11 14:22:51.998 Foo[6923:60b] -[WebApi getSurroundImages] [Line 393] do surround composition
  (lldb) po self.delegate
  [no Objective-C description available]
  </code></pre>
  
  <p>Seems that the object which is not available. What I do not understand is. I am in the SurroundViewController and do activly a refresh by <code>pull-to-refresh</code>. So I am on the Surround View and the object should be available...</p>
  
  <p><strong>How do I fix this issue, that the App does not crash with EXC_BAD_ACCESS at the performSelector line?</strong></p>
  
  <p>Here's the code which is involved with the issue (necessary parts): </p>
  
  <h3>SurroundViewController.h</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import &lt;UIKit/UIKit.h&gt;
  #import "WebApi.h"
  #import "DetailViewController.h"
  #import "SingletonClass.h"
  
  @interface SurroundViewController : UICollectionViewController &lt;WebApiDelegate&gt;
  
  @property (nonatomic, strong) WebApi *swebapi;
  @property (nonatomic, strong) SingletonClass *sshare;
  
  @end
  </code></pre>
  
  <h3>SurroundViewController.m</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import "SurroundViewController.h"
  
  @interface SurroundViewController ()
  
  @property (nonatomic, strong) UIRefreshControl *refresh;
  
  @end
  
  @implementation SurroundViewController
  
  -(void)vinit {
      self.sshare = [SingletonClass sharedInstance];
      self.swebapi = [WebApi sharedInstance];
      self.swebapi.delegate = self;
  }
  
  - (void)viewDidLoad
  {
      [self vinit];
      [self.navigationController setNavigationBarHidden:YES animated:NO];
      [super viewDidLoad];
  
      [self addRefresh];
      [self.swebapi getSurroundImages]; // will call delegate didComposition
  
  }
  
  - (void)viewDidAppear:(BOOL)animated
  {
      [self.navigationController setNavigationBarHidden:YES animated:NO];
  }
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      // cell configuration 
  }
  
  
  -(void)addRefresh {
      UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
      [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
  
      self.refresh = refreshControl;
      [self.collectionView addSubview:self.refresh];
  }
  
  -(void)refresh:(UIRefreshControl*)refresh {
  
      refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing..."];
      [self.swebapi getSurroundImages];
  
  }
  
  -(void)didLoadFoo {
      [self.swebapi doComposition];
  }
  
  - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
      [self performSegueWithIdentifier:@"toDetailView" sender:indexPath];
  }
  
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
      if ([[segue identifier] isEqualToString:@"toDetailView"]) {
          DetailViewController *dvc = [segue destinationViewController];
  
          NSIndexPath *indexPath = sender;
  
         dvc.idx = [self getItemOfSection:indexPath];
         dvc.detailData = [[self.sshare coItem:dvc.idx] mutableCopy];      
      }
  }
  
  - (int)getItemOfSection:(NSIndexPath *)indexPath {
      return (int)indexPath.item + ((int)indexPath.section * 4);
  }
  
  @end
  </code></pre>
  
  <h3>WebApi.h</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import "AFHTTPRequestOperationManager.h"
  #import "Errors.h"
  
  @class WebApi;
  @protocol WebApiDelegate &lt;NSObject&gt;
  
  @optional
  -(void)didLoadFoo;
  
  @end
  
  @interface WebApi : AFHTTPRequestOperationManager &lt;SingletonDelegate&gt;
  
  @property (assign, nonatomic)id&lt;WebApiDelegate&gt; delegate;
  @property (nonatomic, strong) Errors *error;
  
  +(WebApi*)sharedInstance;
  
  -(void)getSurroundStream;
  -(void)getSurroundImages;
  
  @end
  </code></pre>
  
  <h3>WebApi.m</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import "WebApi.h"
  
  #define kApiHost @"http://sample.com"
  #define kApiPath @"sample"
  
  @implementation WebApi
  
  -(WebApi*)initWithBaseURL:url {
      self = [super init];
      if (self != nil) {
          self.sshare = [SingletonClass sharedInstance];
          self.error = [[Errors alloc] init];
      }
      return  self;
  }
  
  +(WebApi*)sharedInstance
  {
      static WebApi *sharedInstance = nil;
      static dispatch_once_t oncePredicate;
      dispatch_once(&amp;oncePredicate, ^{
          sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kApiHost]];
      });
  
      return sharedInstance;
  }
  -(void)getSurroundStream {
  
      NSString *URLString = [NSString stringWithFormat:@"%@/%@/view/%f/%f", kApiHost, kApiPath, self.sshare.coordinate.longitude, self.sshare.coordinate.latitude];
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  
      [self setAuthHeader:manager];
  
      [manager GET:URLString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [self.sshare putViData:responseObject];
  
          [self.delegate performSelector:@selector(didLoadFoo)]; // --&gt; EXC_BAD_ACCESS
  
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          [self.error vError:error message:operation.responseString url:URLString];
      }];
  }
  
  
  -(void)getSurroundImages {
      [self getSurroundStream]; 
  }
  @end
  </code></pre>
  
  <h3>SingletonClass.h</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import &lt;Foundation/Foundation.h&gt;
  
  @class Singleton;
  @protocol SingletonDelegate &lt;NSObject&gt;
  
  -(void)didRefreshToken;
  
  @end
  
  @interface SingletonClass : NSObject
  
  @property (assign, nonatomic) id&lt;SingletonDelegate&gt; delegate;
  
  @property (nonatomic, strong) NSMutableArray *viData;
  @property (nonatomic, strong) NSMutableArray *coData; 
  
  @end
  </code></pre>
  
  <h3>SingletonClasss.m</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import "SingletonClass.h"
  
  @implementation SingletonClass
  
  static SingletonClass *sharedInstance = nil;
  
  // Get the shared instance and create it if necessary.
  + (SingletonClass *)sharedInstance {
      if (sharedInstance == nil) {
          sharedInstance = [[super allocWithZone:NULL] init];
      }
  
      return sharedInstance;
  }
  
  - (id)init
  {
      self = [super init];    
      if (self) {
          self.coData = [[NSMutableArray alloc] init]; 
          self.viData = [[NSMutableArray alloc] init]; 
      }    
      return self;
  }
  
  // We don't want to allocate a new instance, so return the current one.
  + (id)allocWithZone:(NSZone*)zone {
      return [self sharedInstance];
  }
  
  // Equally, we don't want to generate multiple copies of the singleton.
  - (id)copyWithZone:(NSZone *)zone {
      return self;
  }
  -(NSMutableDictionary *)coItem:(int)position {
      NSAssert(self.coData.count &gt; position, @"Position does not exists: coData.count: %lu &gt; position: %d", (unsigned long)self.coData.count, position);
  
      return self.coData[position];
  }
  
  @end
  </code></pre>
  
  <h3>DetailViewController.h</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import &lt;UIKit/UIKit.h&gt;
  #import "SingletonClass.h"
  #import  "WebApi.h"
  
  @interface DetailViewController : UITableViewController &lt;WebApiDelegate&gt;
  
  @property (nonatomic) int idx;
  @property (nonatomic, strong) SingletonClass *sshare;
  @property (nonatomic, strong) WebApi *swebapi;
  @property (nonatomic, strong) NSMutableDictionary *detailData;
  
  @end
  </code></pre>
  
  <h3>DetailViewController.m</h3>
  
  <pre class="lang-c prettyprint-override"><code>#import "DetailViewController.h"
  
  @interface DetailViewController ()
  
  @property (nonatomic, strong) NSArray *cellRows;
  
  @end
  
  @implementation DetailViewController
  
  - (id)initWithStyle:(UITableViewStyle)style
  {
      self = [super initWithStyle:style];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  - (void)vinit {
  
      self.sshare = [SingletonClass sharedInstance];
  
      self.swebapi = [WebApi sharedInstance];
      self.swebapi.delegate = self;
  
      NSAssert(self.detailData, @"detailData is not available");
  }
  
  - (void)viewDidLoad
  {
      [self vinit];
      [self.navigationController setNavigationBarHidden:NO animated:NO];
      [super viewDidLoad];
  
      self.cellRows = @[@"cellLocation:", @"cellIntention:"];
  }
  
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      return self.cellRows.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"detailCell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  
      // Configure the cell...
  
      SEL functionCall = NSSelectorFromString(self.cellRows[indexPath.row]);
      [self performSelector:functionCall withObject:cell];
  
      return cell;
  }
  
  
  - (void)cellLocation:(UITableViewCell*)cell {
      // configuration of table cell    
  }
  
  - (void)cellIntention:(UITableViewCell*)cell {
      // configuration of table cell    
  }
  
  @end
  </code></pre>