title: Cocoa Touch / iOS automatically calling any class static method that is named 'load'
body: <p>I encountered a weird behavior of Cocoa Touch / iOS involving static method. If you implement a static method 'load' in any class, Cocoa Touch / iOS will automatically call that method when the application starts executing. Even if you do not refer to that class from the main execution thread, it'll still be called first, no matter what.</p>
  
  <p>I've managed to replicate the behavior when compiled on SDK 4.2 on Xcode 3.2.5 and SDK 4.3 on Xcode 4.0.1, and when running from both iPhone simulator and iPhone device.</p>
  
  <p>Here's how to replicate:</p>
  
  <p>Create a new class and name it however you want. In the interface declare a static method 'load' (any return type):</p>
  
  <pre><code>+ (void)load;
  </code></pre>
  
  <p>Then implement it:</p>
  
  <pre><code>+ (void)load {
      NSLog(@"What the?");
  }
  </code></pre>
  
  <p>Compile (make sure the new class is compiled along) and run the app (on simulator or device). While the app is starting, switch to and monitor GDB view. You'll see "What the?" echoed there even though by logic no other code refers to the class or the method.</p>
  
  <p>And on top of that, if you use any autorelease code in the method, you'll get an error that shows that the method is called before even the <code>int main()</code> method (the super gateway of all C applications existing out there).</p>
  
  <p>The following code:</p>
  
  <pre><code>+ (void)load {
      NSLog(@"Hello %@", [NSString stringWithString:@"World"]);
  }
  </code></pre>
  
  <p>Will give you something that looks like:</p>
  
  <pre><code>2011-04-21 00:41:53.828 AppName[548:707] *** __NSAutoreleaseNoPool(): Object 0x8fe8c of class NSCFString autoreleased with no pool in place - just leaking
  2011-04-21 00:41:53.838 AppName[548:707] Hello World
  </code></pre>
  
  <p>So the questions are, does anyone know why Cocoa Touch / iOS calls this method? What's so special about method name 'load'? And last but not least, <strong>is this a feature or a bug</strong>?</p>
  
  <p>p/s: I discovered this behavior when it just so happened that the static method 'load' that I implemented instantiates many autoreleased objects and I was perplexed by the multiple <code>__NSAutoreleaseNoPool()</code> warning messages in GDB.</p>