# Csound api and CompileOrc()

**URL:** <https://forum.csound.com/t/csound-api-and-compileorc/1346>\
**Category:** Developers\
**Created:** [September 17, 2023, 2:25pm UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346 "2023-09-17T14:25:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [September 17, 2023, 2:25pm UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/1 "2023-09-17T14:25:52Z")

</div>

I’m developing a plugin for Bidule using their sdk that basically is integrating an instance of Csound api, and will be able to compile any valid orc code that is entered into a text box in the gui.

What’s the standard practice to recompiling orc code using the cpp api, when an instance of Csound is already running?

I have set up the initial orc (and sco) code in the init() method in the plugin, that works fine, but when I recompile some (valid) orc code that replaces the same instr 1, I get some nasty, loud glitchy output and the whole instance basically crashes and eventually brings down Bidule.

Is there a special order to doing this within C++?

I’m not using `compilestr` or any of the Csound opcodes for this - only within C++

This is the general idea:

```csound
// gets called when a button is pressed.
CsoundTest::recompileOrc() {
 
    if (_recompileTrig == 1) {
        // csound->Reset(); <- does this need to be called at all?
        _csCompileResult = csound->CompileOrc(templateOrc.c_str());

        if (_csCompileResult == 0) {
            csound->ReadScore("i1 0 4 220"); // <- restarts a new instance of instr 1
        }
        _recompileTrig = 0;
    }
}

```

thanks!

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [September 18, 2023, 8:39am UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/2 "2023-09-18T08:39:30Z")

</div>

I’ve found the cleanest way to do this is to destroy Csound and create a new instance of it.

```csound
csound = nullptr;
csound = std::make_unique<Csound> ();

```

It’s not pretty, but do the job. I had issues with some resources not being freed when calling `csound->Reset()`, which would in turn lead to other issues.

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [September 18, 2023, 8:50am UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/3 "2023-09-18T08:50:35Z")

</div>

Ah right, brute force! cool will try that.

One thing I did think was, when I recompiled, the orc code was including

```csound
"sr=48000\n\
    ksmps=64\n\
    nchnls=2\n\
    0dbfs=1\n\
    chn_k \"phsRate\", 1\n\
    chn_k \"pitch\", 1\n\
    chn_k \"amp\", 1\n\";

```

each time, rather than just the instr code.

Could that muddle things up?

Thanks

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [September 25, 2023, 1:48pm UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/4 "2023-09-25T13:48:27Z")

</div>

Probably could is that’s what is actually being passed to compileOrc. Sorry, I only noticed this message now. For some reason I don’t get notified of reponses…

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [September 25, 2023, 2:26pm UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/5 "2023-09-25T14:26:21Z")

</div>

Haha is ok! i think there is some notification button somewhere that gets auto-checked when you are the author…i dunno. Notifications sometimes do, sometimes don’t work…

In the end I decided for simplicity to pass the path to a .csd, and compile that instead. The code editing ui facilities within Bidule is somewhat lacking, so prob best to leave that to a dedicated editor for now. But I may circle back to this when I have it all working how I want, but I suspect that is a can of worms.

Cheers

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [September 27, 2023, 8:23am UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/6 "2023-09-27T08:23:34Z")

</div>

Quick one @rory

By doing `csound = nullptr;` does that leave any memory leaks/un-deleted resources lying around?

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [September 27, 2023, 9:32am UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/7 "2023-09-27T09:32:53Z")

</div>

It shouldn’t, as this will call the Csound destructor, which in turn should destroy all resources.

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [September 27, 2023, 10:22am UTC](https://forum.csound.com/t/csound-api-and-compileorc/1346/8 "2023-09-27T10:22:18Z")

</div>

if it’s in a smart pointer, right?

also does there need to be a period of time between destroying the instance of Csound, and then recreating it? probably something in my code, but I get a message from Csound saying: `Csound is already started, call csoundReset() before starting again.` Makes me think is “it” being called twice… it being `csound->Start()`?
