Attention! For Japanese customers, my Japanese blog might help.

Jul 21, 2016

AudioComponentInstanceNew() returns kAudioUnitErr_CannotDoInCurrentContext for v3 AudioUnits.

NOTE: This is the English version of my Japanese article.

I was looking for the reason that AudioComponentInstanceNew() returned -10863(kAudioUnitErr_CannotDoInCurrentContext) for an AudioUnit, which was created using target template on Xcode 7.3.

The AudioUnit was also failed a validation with auvaltool -v.

$ auvaltool -v aufx vibr Symi

    AU Validation Tool
    Version: 1.6.1a1 
    Copyright 2003-2013, Apple Inc. All Rights Reserved.
    Specify -h (-help) for command options

--------------------------------------------------
VALIDATING AUDIO UNIT: 'aufx' - 'vibr' - 'Symi'
--------------------------------------------------
Manufacturer String: Shakeyama
AudioUnit Name: VibratoUnit
Component Version: 1.6.0 (0x10600)

* * PASS
--------------------------------------------------
TESTING OPEN TIMES:
COLD:
Time to open AudioUnit:         333.540 ms
WARM:
Time to open AudioUnit:         101.261  ms
This AudioUnit is a version 3 implementation.
FIRST TIME:
FATAL ERROR: Initialize: result: -50

From the message above, it was found that the AudioUnit seemed version 3.
For version 3 AudioUnit, for example, it can be initialized using AVAudioUnit.instantiateWithComponentDescription() in Swift.
Reference: Shared/SimplePlayEngine.swift

After further survey, I confirmed that the AU can be initialized using +[AUAudioUnit instantiateWithComponentDescription:options:completionHandler:] in Obj-C, or AudioComponentInstantiate() in C, as well.

Note that these initialization was done asynchronously.
So, you will need modify your host code to write post-initalization code in Blocks.

By the way, if you want to make version 2 AudioUnit, Audio Unit Examples (AudioUnit Effect, Generator, Instrument, MIDI Processor and Offline) may help.
I think this way will be easer...