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?
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.
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];
}
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.