2

I have a C function(a callback function in Audio Queue Services) and I want to send message to self. How to do that? If I type in [self message], there is an error:

Use of undeclared identifier 'self'
yoyosir
  • 458
  • 2
  • 11
  • 27
  • provide more detail, by self you mean your class right? cant you just [self message]? since even if its a c function you can add objective c code. or by self do you mean your function as in recursion? – Pochi Apr 30 '12 at 04:52
  • possible duplicate of [Calling a Objective C function from C++ Code](http://stackoverflow.com/questions/6958685/calling-a-objective-c-function-from-c-code) – Caleb Apr 30 '12 at 05:04

2 Answers2

4

You don't perform objc messages in realtime callbacks, such as audio. The reason for this is that objc messaging is not constant time, and may lock, resulting in missed deadlines, resulting in audio dropouts. Static or dynamic (virtual) C++ calls and C function calls are of course constant time, and suitable for realtime contexts.

If it were not an realtime/audio callback, then one option would be to pass self as the user info or context parameter:

void SomeCallback(t_stuff* const stuff, void* userInfo) {
  MONClass * Self = (MONClass*)userInfo;
  [Self doSomething];
}
justin
  • 104,054
  • 14
  • 179
  • 226
  • Actually I am writing 'HandleInputBuffer' function where there seems to be no way to add a parameter like this. Is there another way? – yoyosir Apr 30 '12 at 05:01
  • @user1214321 Follow the link to the duplicated question for an example. – Caleb Apr 30 '12 at 05:07
  • @user1214321 `HandleInputBuffer` is, apparently, the name of some function from some apple sample code or demo/docs -- the `HandleInputBuffer` function is used as an `AudioQueueInputCallback`. As an `AudioQueueInputCallback`, the first parameter `void* inUserData` would be the parameter I am referring to. – justin Apr 30 '12 at 05:07
  • Thanks for explaining this. Do you mean that there is no way to analyze the recorded audio data in an obj-C way other than save them to a file first? – yoyosir Apr 30 '12 at 05:09
  • @Caleb good find! unfortunately, neither answer at the other question notes the consequence of locking in audio callbacks :( – justin Apr 30 '12 at 05:13
  • @user1214321 you *could* use an objc instance for the `userInfo` parameter if you handle it strictly as you would a c struct -- direct ivar accesses, c functions, no objc messaging, no locking, no allocations, etc. in the callback. i've seen it before. so, you can analyze it using c and c++, or you can pass the objc object and handle it using pure c. also note that ARC would be forbidden in this context. – justin Apr 30 '12 at 05:16
  • @user1214321 since audio queues often use 3 buffers, your callback may not actually be performed on the realtime audio thread. but you need to confirm this elsewhere. – justin Apr 30 '12 at 05:20
  • @Justin,so is it better to first write them to a audio file, and then read from the file and analyze it? – yoyosir Apr 30 '12 at 07:44
  • @user1214321 maybe. maybe not. unanswerable. not enough context. – justin Apr 30 '12 at 07:48
  • Some suspect assertions in this answer. Objective C messaging itself is no different from C or C++ function calls underneath, and you're also confusing the issue of constant time. There is nothing in the objective C dispatch procedure that would necessarily lock. Now, calling framework methods from a realtime audio callback might cause problems, sure… not least of which because audio callbacks are unlikely to be on the main thread. – bleater Feb 25 '14 at 21:12
3

self is only meaningful in the context of a class definition -- it's a pointer to an instance of that class. Functions aren't part of any class, so there is no self pointer. If you want your callback to be able to send a message to a given object, you'll need to stash a pointer to that object where your callback can find it. That might be a global variable (ick), a userInfo parameter that's meant for just this sort of thing (much better), or somewhere else.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    +1 for giving the reason why you *can't* use `self` in a C function (the accepted answer only says why you shouldn't use Objective-C message passing in audio callbacks, not why you can't reference `self`) – JeremyP Apr 30 '12 at 09:18