NSInvocation problem - "NSView not correctly initialized"

  • Hi all,

    The following test program demonstrates a problem I'm having: when I
    call [NSButton alloc] through an NSInvocation, I get this error message:

    2007-11-04 22:55:22.631 buttons[8483] NSButton(0x314150) - NSView not
    correctly initialized. Did you forget to call super?

    Strangely, the button seems to function fine, despite the message.
    Is there something wrong with the way I'm using NSInvocation?

    thanks,
    Rob

    #import <Cocoa/Cocoa.h>

    int main()
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSApplication *app = [NSApplication sharedApplication];

        id b;
        SEL alloc_sel = @selector(alloc);
        id button_class = NSClassFromString(@"NSButton");

        printf("Allocate button normally...\n");
        b = [NSButton alloc];
        printf("  done\n\n");

        printf("Try objc_msgSend...\n");
        b = objc_msgSend(button_class, alloc_sel);
        printf("  done\n\n");

        printf("Try with NSInvocation...\n");
        id sig = [button_class methodSignatureForSelector: alloc_sel];
        id inv = [NSInvocation invocationWithMethodSignature: sig];
        [inv setSelector: alloc_sel];
        [inv setTarget: button_class];
        [inv invoke];
        [inv getReturnValue: &b];
        printf("  done\n\n");

        [pool release];
    }
  • Hi Robert,

    As the message says: "NSView not correctly
    initialized", you are only allocating the instance of
    NSButton. ObjC requires an additional -init message to
    be send to it for initialzation. NSButton expects
    -initWithFrame: at least, to correctly initialize its
    NSView superclass.

    I am not at my computer right now, so I do not
    remember NSButton's designated initializer, but for
    NSView the sequence is:
    NSView *myView = [[NSView alloc]
    initWithFrame:someFrame];

    HTH
    Gorazd

          Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com
  • On 11/4/07, Robert Nikander <nikander...> wrote:
    > Hi all,
    >
    > The following test program demonstrates a problem I'm having: when I
    > call [NSButton alloc] through an NSInvocation, I get this error message:
    >
    > 2007-11-04 22:55:22.631 buttons[8483] NSButton(0x314150) - NSView not
    > correctly initialized. Did you forget to call super?

    You never called any of the "init..." methods. Unless you *really*
    know what you're doing, a call to +alloc should always be followed by
    a call to one of the class' init methods.

    --
    Clark S. Cox III
    <clarkcox3...>
  • On Nov 5, 2007, at 10:43 AM, Clark Cox wrote:
    > You never called any of the "init..." methods.

    On Nov 5, 2007, at 10:15 AM, Gorazd Krosl wrote:
    > ObjC requires an additional -init message to
    >

    Unfortunately, this is not the problem...  In real code I call
    "init...", but I removed those calls from the demo program to show
    the problem with the least amount of code.  I forgot to mention that
    the error only occurs in the final call to "alloc" -- the call via
    the NSInvocation.  The first two calls to "alloc" are fine, as they
    should be, since it is not an error to call [NSButton alloc].  It is
    only an error to try to use the object (ie, call other methods on it)
    without first calling "init...".

    So, these lines do not cause an error message:

        b = [NSButton alloc];
        b = objc_msgSend(button_class, alloc_sel);

    but this line does:

      [inv invoke];

    despite the fact that they are, I think (?), doing the same thing.
    So what is different about calling through the NSInvocation that
    causes the error message?

    thanks,
    Rob
  • On 11/5/07, Robert Nikander <nikander...> wrote:

    > So what is different about calling through the NSInvocation that
    > causes the error message?

    As a test try sending a retain message to alloced object after each of
    the following...

      b = [NSButton alloc];
      b = objc_msgSend(button_class, alloc_sel);

    -Shawn
  • On Nov 5, 2007, at 12:37 PM, Shawn Erickson wrote:
    > On 11/5/07, Robert Nikander <nikander...> wrote:
    >
    >> So what is different about calling through the NSInvocation that
    >> causes the error message?
    >
    > As a test try sending a retain message to alloced object after each of
    > the following...
    >
    > b = [NSButton alloc];
    > b = objc_msgSend(button_class, alloc_sel);

    Doh!  Thanks, I think that answers it.  It makes sense that
    NSInvocation would retain object return values, and is thus unusable
    for calling "alloc".  I'm used to garbage collection...

    Rob
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today