title: What is NSString in struct?
body: <p>I've defined a struct and want to assign one of its values to a NSMutableDictionary.  When I try, I get a EXC_BAD_ACCESS.  Here is the code:</p>
  
  <pre><code>//in .h file
  typedef struct {
  NSString *valueOne;
  NSString *valueTwo;
  }  myStruct;
  
  myStruct aStruct;
  
  //in .m file
  - (void)viewDidLoad {
  [super viewDidLoad];
  aStruct.valueOne = @"firstValue";
  }
  
  //at some later time
  [myDictionary setValue:aStruct.valueOne forKey:@"key1"]; //dies here with EXC_BAD_ACCESS
  </code></pre>
  
  <p>This is the output in debugger console:</p>
  
  <pre><code>(gdb) p aStruct.valueOne
  $1 = (NSString *) 0xf41850
  </code></pre>
  
  <p>Is there a way to tell what the value of aStruct.valueOne is?</p>
  
  <p>Since it is an NSString, why does the dictionary have such a problem with it?</p>
  
  <p>------------- EDIT -------------</p>
  
  <p>This edit is based on some comments below.</p>
  
  <p>The problem appears to be in the struct memory allocation.  I have no issues assigning the struct value to the dictionary in viewDidLoad, as mentioned in one of the comments.  The problem is that later on, I run into an issue with the struct.  Just before the error, I do:</p>
  
  <pre><code>po aStruct.oneValue
  
  Program received signal EXC_BAD_ACCESS, Could not access memory.
  Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
  0x9895cedb in objc_msgSend ()
  The program being debugged was signaled while in a function called from GDB.
  GDB has restored the context to what it was before the call.
  To change this behavior use "set unwindonsignal off"
  Evaluation of the expression containing the function (_NSPrintForDebugger) will be   abandoned.
  </code></pre>
  
  <p>This occurs just before the EXC_BAD_ACCESS:</p>
  
  <pre><code>NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  [formatter setDateFormat:@"MM-dd-yy_HH-mm-ss-A"];
  NSString *date = [formatter stringFromDate:[NSDate date]];
  [formatter release];
  </code></pre>
  
  <p>aStruct.valueOne = date;</p>
  
  <p>So the memory issue is most likely in my releasing of formatter.  The date var has no retain.  Should I instead be doing</p>
  
  <pre><code>NSString *date = [[formatter stringFromDate:[NSDate date]] retain];
  </code></pre>
  
  <p>Which does work but then I'm left with a memory leak.</p>