title: How do I return a struct value from a runtime-defined class method under ARC?
body: <p>I have a class method returning a <code>CGSize</code> and I'd like to call it via the Objective-C runtime functions because I'm given the class and method names as string values.</p>
  
  <p>I'm compiling with ARC flags in XCode 4.2.</p>
  
  <p>Method signature:</p>
  
  <pre><code>+(CGSize)contentSize:(NSString *)text;
  </code></pre>
  
  <p>The first thing I tried was to invoke it with <code>objc_msgSend</code> like this:</p>
  
  <pre><code>Class clazz = NSClassFromString(@"someClassName);
  SEL method = NSSelectorFromString(@"contentSize:");
  
  id result = objc_msgSend(clazz, method, text);
  </code></pre>
  
  <p>This crashed with "EXC_BAD_ACCESS" and without a stack trace. I used this first because the documentation for <code>objc_msgSend</code> says, </p>
  
  <blockquote>
    <p>When it encounters a method call, the compiler generates a call to one of the functions <code>objc_msgSend</code>, <code>objc_msgSend_stret</code>, <code>objc_msgSendSuper</code>, or <code>objc_msgSendSuper_stret</code>. [...] Methods that have data structures as return values are sent using <code>objc_msgSendSuper_stret</code> and <code>objc_msgSend_stret</code>.</p>
  </blockquote>
  
  <p>Next, I used <code>objc_msgSend_stret</code> like this:</p>
  
  <pre><code>Class clazz = NSClassFromString(@"someClassName);
  SEL method = NSSelectorFromString(@"contentSize:");
  
  CGSize size = CGSizeMake(0, 0);
  objc_msgSend_stret(&amp;size, clazz, method, text);
  </code></pre>
  
  <p>Using the above signature gives me the following two compiler errors and two warnings:</p>
  
  <blockquote>
    <p>error: Automatic Reference Counting Issue: Implicit conversion of a non-Objective-C pointer type 'CGSize *' (aka 'struct CGSize *') to 'id' is disallowed with ARC  </p>
    
    <p>warning: Semantic Issue: Incompatible pointer types passing 'CGSize *' (aka 'struct CGSize *') to parameter of type 'id'</p>
    
    <p>error: Automatic Reference Counting Issue: Implicit conversion of an Objective-C pointer to 'SEL' is disallowed with ARC  </p>
    
    <p>warning: Semantic Issue: Incompatible pointer types passing 
        '__unsafe_unretained Class' to parameter of type 'SEL'</p>
  </blockquote>
  
  <p>If I look at the declaration of the method, it is:</p>
  
  <pre><code>OBJC_EXPORT void objc_msgSend_stret(id self, SEL op, ...)
      __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
  </code></pre>
  
  <p>which is identical to <code>objc_msgSend</code>:</p>
  
  <pre><code>OBJC_EXPORT id objc_msgSend(id self, SEL op, ...)
      __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
  </code></pre>
  
  <p>This explains the compiler errors to me but what do I use at the run-time level to invoke a class and its static method at run-time and return a struct value?</p>