Re: Passing C Style Function Callbacks in Cocoa
Re: Passing C Style Function Callbacks in Cocoa
- Subject: Re: Passing C Style Function Callbacks in Cocoa
- From: Vince Ackerman <email@hidden>
- Date: Tue, 6 Apr 2004 18:28:03 -0700
Well, I thought I had solved my problem with passing C style function
callbacks and then being able to find the appropriate object/method. I
am using the technique suggest by Hiro Fujimoto:
static id myObject10 // Declared in top of each object's .m file
outside the objects methods
static id myObject11 // etc
// As each IODinObj device object is initialized, I assign one of the
static myObjectX's to it
- (id) init
{
self = [super init];
// Check if another IODin Device was created and is using myObject(x)
// If not, assign it to this IODinObj
if (myObject10 == nil)
myObject10 = self;
else if (myObject11 == nil)
myObject11 = self;
else if (myObject12 == nil)
myObject12 = self;
else
myObject13 = self;
return self;
}
// This is the callback passed to the USB device:
TeleoError IODinValueCB( TMioDin* tmdin, bool aValue)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // fixes
memory leak for some reason
// Find out which IODinObj is making the callback
if ( [myObject10 IODinDevice] == tmdin)
[myObject10 updateValue: aValue];
else if ( [myObject11 IODinDevice] == tmdin)
[myObject11 updateValue: aValue];
else if ( [myObject12 IODinDevice] == tmdin)
[myObject12 updateValue: aValue];
else
[myObject13 updateValue: aValue];
[pool release];
return TELEO_OK;
}
I have about 10 of the exact same callback functions throughout my code
and most work without error, however this one which used to work now
crashes every time it's called. Don't know what changed, but maybe it
was only luck that made it work to begin with. Can anyone suggest what
I'm doing wrong?
Stack trace:
#0 0x908311ec in objc_msgSend // <--- Disassembles here (below)
#1 0x004d56a0 in IODinValueCB at IODinObj.m:37 // <----- this is my C
style callback that was passed earlier to the USB Device
#2 0x01748df8 in TMioDin_propertyUpdate at TeleoMioDin.c:332 // <----
this is third party USB device code
0x908311e0 <+0000> cmplwi r3,0
0x908311e4 <+0004> beq- 0x90831308 <objc_msgSend+296>
0x908311e8 <+0008> lwz r12,0(r3)
0x908311ec <+0012> lwz r12,32(r12) <----------Crashes here with:
"Program received signal: "EXC_BAD_ACCESS". "
0x908311f0 <+0016> stw r9,48(r1)
0x908311f4 <+0020> lwz r11,0(r12)
0x908311f8 <+0024> addi r9,r12,8
0x908311fc <+0028> rlwinm r11,r11,2,0,29
0x90831200 <+0032> and r12,r4,r11
0x90831204 <+0036> lwzx r2,r9,r12
0x90831208 <+0040> addi r12,r12,4
0x9083120c <+0044> cmplwi r2,0
0x90831210 <+0048> beq- 0x90831234 <objc_msgSend+84>
Thanks in advance
Vince
On Mar 5, 2004, at 08:56, John Nairn wrote:
>
On Mar 5, 2004, at 8:42 AM, email@hidden wrote:
>
>
> 'm a Object C newbie and hope someone here can help. I need to pass a
>
> function call-back to a C- style function from one of my objects. I
>
> have declared the call-back function before the @implementation MyObj.
>
> The problem I'm having is accessing my object from within the
>
> call-back
>
> function, which doesn't seem to know about my object. I want the
>
> callback to send a setter message to my object. I'd rather be able to
>
> pass a call-back that was a method of my object instead. Is there a
>
> way
>
> to pass a Obj C style message as a function* in place of the C-style
>
> function???
>
>
There are probably several solutions, but one I found very powerful
>
was to set up a delegate option analogous to many Cocoa classes. You
>
set the delegate of an object which will be the object to receive the
>
call backs. The object making the callbacks can then inquire of the
>
delegate if it supports any method (try to create an NSMethodSignature
>
using [delegate methodSignatureForSelector:mySelector]). If it has the
>
method you can create an NSInvocation for that signature, set its
>
target to the delegate, and its selector to the method you want to
>
call.
>
>
NSInvocation *newMethod=[NSinvocation
>
invocationWithMethodSignature:signature];
>
[newMethod setTarget:delegate];
>
[newMethod setSelector:mySelector];
>
>
Finally, when you want to do a callback, fill in any number of
>
arguments and "invoke" that NSInvocation.
>
>
I used this method to make a custom parser for GEDCOM genealogy files.
>
It sends call backs as it encounters new records, new lines of data,
>
errors in the data, or various other data-specific items. It is
>
similar to some built-in Cocoa parsers such as the XML parser, but
>
customized for the data type I am reading. The method is very general.
>
>
------------
>
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
>
Web page: http://www.eng.utah.edu/~nairn
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.