Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does NSInvocation getReturnValue: lose object?
    text
    copied!<p>I need your help. I have some problems with NSInvocation 'getReturnValue:' method. I want to create UIButton programmatically, and even more, I want to create it dynamically using NSInvocation and through passing value through the NSArray (that's why I wrapped UIButtonTypeRoundedRect).</p> <p>Listing.</p> <pre><code>NSLog(@"Button 4 pushed\n");//this code executed when button pushed Class cls = NSClassFromString(@"UIButton");//if exists {define class},else cls=nil SEL msel = @selector(buttonWithType:); //id pushButton5 = [cls performSelector:msel withObject:UIButtonTypeRoundedRect];//this code works correctly,but I want to do this by NSInvocation //--------------------------- NSMethodSignature *msignatureTMP; NSInvocation *anInvocationTMP; msignatureTMP = [cls methodSignatureForSelector:msel]; anInvocationTMP = [NSInvocation invocationWithMethodSignature:msignatureTMP]; [anInvocationTMP setTarget:cls]; [anInvocationTMP setSelector:msel]; UIButtonType uibt_ = UIButtonTypeRoundedRect; NSNumber *uibt = [NSNumber numberWithUnsignedInt:uibt_]; NSArray *paramsTMP; paramsTMP= [NSArray arrayWithObjects:uibt,nil]; id currentValTMP = [paramsTMP objectAtIndex:0];//getParam from NSArray NSInteger i=2; void* bufferTMP; //if kind of NSValue unwrapp it. if ([currentValTMP isKindOfClass:[NSValue class]]) { NSUInteger bufferSize = 0; NSGetSizeAndAlignment([currentValTMP objCType], &amp;bufferSize, NULL); bufferTMP = malloc(bufferSize); [currentValTMP getValue:bufferTMP];//copy currentVal to bufer [anInvocationTMP setArgument:bufferTMP atIndex:i];// The +2 represents the (self) and (cmd) offsets }else { [anInvocationTMP setArgument:&amp;currentValTMP atIndex:i];//Again,+2 represents (self) and (cmd) offsets } void* result = malloc([[cls methodSignatureForSelector:msel] methodReturnLength]); [anInvocationTMP invoke]; [anInvocationTMP getReturnValue:result]; NSLog(@"sizeof(UIButton)=%i,sizeof(result)=%i,methodreturnlength = %i,sizeof(*result)=%i",class_getInstanceSize(NSClassFromString(@"UIButton")),sizeof(result),[[cls methodSignatureForSelector:msel] methodReturnLength],sizeof(*result)); id pushButton5; pushButton5=result; //--------------------------- </code></pre> <p>NSLog output:<b> sizeof(UIButton)=140,sizeof(result)=4,methodreturnlength = 4,sizeof(*result)=1</b></p> <p>The problem is that value from NSInvocation is pointer of size 4 bytes. It should point to UIButton object,size of 140 bytes. But actually refers to 1 byte data. So what does happen with UIButton object,that should be initialized by 'buttonWithType:'?</p> <h2>Added after getting some answers:</h2> <p>To clarify: I want to get <code>UIButton</code> object, but after this code <code>id pushButton5 = (id) result;</code> ,when I try to work with <code>pushButton5</code>,it causes <code>EXC_BAD_ACCESS</code>. Can someone help me?</p> <p>Can this happen because of this? </p> <pre><code>Class cls = NSClassFromString(@"UIButton"); ... [anInvocationTMP setTarget:cls]; </code></pre> <p>It is correct, isn't it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload