• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Access Obj-C method/attribute from a C method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Access Obj-C method/attribute from a C method


  • Subject: Re: Access Obj-C method/attribute from a C method
  • From: Alastair Houghton <email@hidden>
  • Date: Sat, 7 Jul 2007 08:30:33 +0100

On 7 Jul 2007, at 08:13, Eric MORAND wrote:

I was wondering it it was possible to call an Obj-C method or access Obj-C instance attributes from inside a C method.

Yes, of course.

In my implementation, I have to use some C methods (because I'm dealing with IOKit) and need to add some objects in fooArray from inside my C method :

void ObjectAdded(void * refCon, io_iterator_t iterator)
{
	// A lot of things are done...

	[self addObjectToFooArray:anObject];
}

Unfortunately, it doesn't compile :

error: 'self' undeclared (first use in this function)

You can't use "self" from a C function like that, because the C function is just a function and could be called from anywhere. There are, broadly speaking, a few possible solutions to this problem:


1. Often, C APIs that use callbacks will allow you to specify an arbitrary void * pointer that gets passed to the callback when it is invoked. You can set this to self when you call the C API from your method implementation, then cast it back to the correct type in your callback function. If Apple have designed APIs that *don't* allow you to specify such a thing, consider filing a bug report.

2. Depending on the design of your application, the object you need to send messages to might be a singleton. If so, you can do something like

  [[MyClass sharedObjectOfMyClass] myMethod];

which solves the problem rather nicely.

3. You could store a pointer to the object in a global variable. Obviously if you do this, you won't be properly thread safe. You could potentially address thread safety by storing your object pointer e.g. in the NSThread -threadDictionary. You might still cause yourself trouble if you ever re-enter your own code on the same thread (i.e. you call a method on one instance, which, during its execution, calls a method on another instance, thus altering the global pointer).

4. You could use the "nested functions" GCC extension. *However*, because GCC's implementation requires that the system stack be executable (which is a potential security hole), it's generally discouraged. (See <http://developer.apple.com/technotes/tn2006/ tn2161.html>).

As far as accessing member variables goes, if your function is between an @implementation and @end in your code, it will have the same access to member variables as a method on the object the @implementation is for. Obviously you'll have to do e.g. myObject- >memberVariable rather than just writing memberVariable. It's often better to use accessors, of course.

Kind regards,

Alastair.

--
http://alastairs-place.net




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Access Obj-C method/attribute from a C method (From: Eric MORAND <email@hidden>)

  • Prev by Date: Re: Access Obj-C method/attribute from a C method
  • Next by Date: Re: Access Obj-C method/attribute from a C method
  • Previous by thread: Re: Access Obj-C method/attribute from a C method
  • Next by thread: Re: Access Obj-C method/attribute from a C method
  • Index(es):
    • Date
    • Thread