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: Clark Cox <email@hidden>
- Date: Thu, 8 Apr 2004 08:06:42 -0400
On Apr 07, 2004, at 19:49, Hiro Fujimoto wrote:
>
Hi. Vince,
>
>
I'm not sure this is the answer to your problem, but... I guess.
>
>
* You should not assign return value of [super init] to self.
No, you should *always* assign the result of [super init] to self.
>
>
On 2004/04/07, at 10:28, Vince Ackerman wrote:
>
>
> 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;
>
> }
To the OP:
Likely what is happening is that when you create these instances, you
are not retaining them properly, and they are eventually getting
deallocated. Then, by the time that your callbacks are called, the
objects no longer exist, and your myObjectXX pointers point to garbage.
>
> // 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;
>
> }
What's wrong with:
TeleoError IODinValueCB( TMioDin* tmdin, bool aValue)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // fixes
memory leak for some reason
[tmdim updateValue: aValue];
[pool release];
return TELEO_OK;
}
--
Clark S. Cox III
email@hidden
http://homepage.mac.com/clarkcox3/
http://homepage.mac.com/clarkcox3/blog/B1196589870/index.html
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.