tags:

views:

4320

answers:

5

Hi, I am using MVC architecture for a GUI application. The model class has some C functions. One of the C functions calls some methods of Objective-C class. I call those methods using an object of that class. The strange thing happening is that methods previously to the an xyz method are called perfectly but when that xyz method is called, that method and the methods after it aren't getting executed. I don't get any errors. So can't figure out what exactly is happening. Any ideas as to what might be the reason for this?

A: 

You can definitely run methods on Obj-C objects from a C function, so it's likely there's an error in your code at some point. If I had to guess, it sounds like an object is nil when you don't expect it to be. Typically you'd start by setting breakpoints in your function, so you can find out what the state is of the objects in question.

Marc Charbonneau
+11  A: 

As Marc points out, you're probably using a reference to the OBJC object that is un-initialised outside the objective-c scope.

Here's a working sample of C code calling an ObjC object's method:

#import <Cocoa/Cocoa.h>

id refToSelf;

@interface SomeClass: NSObject
@end

@implementation SomeClass
- (void) doNothing
{
        NSLog(@"Doing nothing");
}
@end

int otherCfunction()
{
        [refToSelf doNothing];
}


int main()
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    SomeClass * t = [[SomeClass alloc] init];
    refToSelf = t;

    otherCfunction();

    [pool release];
}
diciu
dude, you rock. I wasted a good day on this and your code just salvaged an otherwise crappy day. I wish I could upvote you 100 more times.
eviljack
Another 100 upvotes from me! This was incredibly helpful.
willc2
BTW this will only work for a singleton. If you instantiate more than one object that refToSelf pointer will only point to the last object.
Zaph0d42
@Zaph0d42 - You're right. If a C callback that calls into multiple instances is needed, then the C function can have a void * argument for the ObjC reference to be passed through.
diciu
A: 

www.illudengine.com - look at the article here - i think that will help you

A: 

Breakpoint on the method before the xyz, and then start single stepping in the debugger. Its amazing how many bugs you can spot by single stepping through the code in the debugger. In fact, this is actually a recommended bug avoidance technique to always single step through the code for any new code you write.

I agree with Marc, the classic cause of "that method and the methods after it aren't getting executed" is that the object reference is nil, since [nil doAnything] will do nothing and report no error.

Peter N Lewis