On Saturday, 7 June 2014 at 21:18:39 UTC, Denis Martinez wrote:
2. Is it possible to make a delegate to be allocated on the heap ?
I think of a syntax such as:
client.process_callback = new delegate int(jack_nframes_t nframes) {

AFAIK, no.

3. I have coded a workaround for the time being.
The function here copies the delegate into a heap-allocated structure, and takes the interior pointer.

  T *copyDelegate(T)(T dg) {
    struct Tmp { T dg; }
    auto x = new Tmp;
    x.dg = dg;
    return &x.dg;
  }

I'll have to note that if you're passing this somewhere in C, D won't be keeping track of it automatically anymore. Thus, it's very likely your delegate will be garbage collected at some point (thus the memory might be repurposed for other things causing a similar issue to what you're having now).

Ultimately, I think if you want to keep the delegate around it's probably simpler to store it somewhere and use the pointer to that somewhere. See: http://dlang.org/interfaceToC.html for more info.

Reply via email to