15

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?

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
shrads
  • 521
  • 3
  • 9
  • 16
  • Can you post some code? Otherwise it will be impossible to help. – Jason Coco Nov 20 '08 at 10:34
  • I second that request. Some simple code will be very helpful. E.g. just some code of the C function showing how you actually access the obj-c object and how you make the call to it could be very helpful – Mecki Nov 20 '08 at 10:54

4 Answers4

56

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
  • 29,133
  • 4
  • 51
  • 68
  • 2
    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 Dec 17 '09 at 00:31
  • 1
    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. – Brian Mar 31 '10 at 19:00
  • @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 Apr 01 '10 at 04:24
  • Ohmygoodness. For the love of Space-Jesus, please just make `id self` a parameter of `otherCfunction`. Aqua-Buddha will surely not extend your seed into eternity otherwise. – Jonathan Sterling Jan 11 '11 at 03:41
  • 1
    @Jonathan Sterling - you assume you control the prototype of the C function and that is not always the case. Mixing ObjC/C calls is likely to be related to a callback function and in some cases the callback signature does not include the usual void * argument that would allow passing the handle of a ObjC segment over. – diciu Jan 11 '11 at 07:52
1

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
  • 17,664
  • 2
  • 43
  • 56
1

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
  • 40,399
  • 3
  • 75
  • 82
0

There's a simple way to do this:

@implementation
// Define a object
ClassName *thisClass;

and then init with self

thisClass = self;

You can use any var and methods in C-Method

NoWall
  • 1