title: EXC_BAD_ACCESS error in Objective-C
body: <p>Every now and then I get one of the <code>EXC_BAD_ACCESS</code> errors that seem to plague new Objective-C programmers.  </p>
  
  <p>In my program I'm trying to get a get the time in seconds, convert that to a string and then  convert that to a <code>NSData</code> object for writing to a file. Here is the code I'm using, but it crashes with an <code>EXC_BAD_ACCESS</code> every time I run it.  What am I doing wrong?</p>
  
  <pre><code>-(void) startTheClock{
  
      NSTimeInterval cloqInTime = [NSDate timeIntervalSinceReferenceDate];
      NSString * dateStr = [self stringFromTimeInterval:cloqInTime];
  
      NSData * data = [[dateStr stringByAppendingString:@", "] dataUsingEncoding:NSUTF8StringEncoding];
      NSLog([@"Data:" stringByAppendingString:[data description]]);
  
  
  // [data retain]; // &lt;-- Uncommenting this and the [data release] line doesn't prevent the error
     [self writeData:data]; // &lt;-- EXC_BAD_ACCESS happens here!
  // [data release];
  }
  </code></pre>
  
  <p>When I run this method I get the following output:</p>
  
  <pre><code>timeString: |2011-11-04 16:17:12|
  Data:&lt;32303131 2d31312d 30342031 363a3137 3a31322c 20&gt;
  </code></pre>
  
  <p>As requested here is my <code>stringFromTimeInterval</code> method:</p>
  
  <pre><code>-(NSString *) stringFromTimeInterval:(NSTimeInterval)t{
      NSDate * date  = [NSDate dateWithTimeIntervalSinceReferenceDate:t];
  
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  
      NSString *timeString = [dateFormat stringFromDate:date];
  
      NSLog(@"timeString: %@", timeString);    
      return timeString;
  }
  </code></pre>
  
  <p>Here is the stacktrace:</p>
  
  <pre><code>#0  0x00007fff84407e90 in objc_msgSend ()
  #1  0x00000001054543f0 in 0x00000001054543f0 ()
  #2  0x00000001000012ee in -[WorqAppDelegate startTheClock] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:61
  #3  0x0000000100001164 in -[WorqAppDelegate cloqInAction:] at /Users/slayton/Documents/Xcode/Worq/Worq/WorqAppDelegate.m:37
  #4  0x00007fff8cbb9a1d in -[NSObject performSelector:withObject:] ()
  #5  0x00007fff86f69710 in -[NSApplication sendAction:to:from:] ()
  #6  0x00007fff86f69642 in -[NSControl sendAction:to:] ()
  #7  0x00007fff86f6956d in -[NSCell _sendActionFrom:] ()
  #8  0x00007fff86f68a30 in -[NSCell trackMouse:inRect:ofView:untilMouseUp:] ()
  #9  0x00007fff86fe88e0 in -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] ()
  #10 0x00007fff86f6763a in -[NSControl mouseDown:] ()
  #11 0x00007fff86f320e0 in -[NSWindow sendEvent:] ()
  #12 0x00007fff86eca68f in -[NSApplication sendEvent:] ()
  #13 0x00007fff86e60682 in -[NSApplication run] ()
  #14 0x00007fff870df80c in NSApplicationMain ()
  #15 0x0000000100001092 in main ()
  </code></pre>
  
  <p>Here is the <code>writeData</code> method, <code>recordFile</code> is an instance of <code>NSFileHandle</code>:</p>
  
  <pre><code>-(BOOL) writeData:(NSData *)data{
      if (recordFile != NULL)
          [recordFile writeData:data];
      else
          NSLog(@"Record file is null! No data written");
  }
  </code></pre>