Closures and Memory Management

Andrew Wiley wiley.andrew.j at gmail.com
Wed Apr 20 16:07:43 PDT 2011


I'm working on a project that looks like it'll need manual memory management
(the end goal is to get it running on ARM using GDC, where the GC doesn't
seem to behave (that goal might be unrealistic, but I can hope)), and I'm
trying to figure out how to deal with closures. My understanding is that a
closure is a function pointer and the enclosing scope, whether that scope is
variables that are copied into the closure or a class instance to use as
"this." Is this correct?

Assuming that's correct, this would involve a memory allocation, right?
----------
class Test {
void doStuff() {}
}

void doSomethingElse(void delegate() thing) {
thing();
}

void main() {
auto test = new Test();
doSomethingElse(&test.doStuff);
}
----------

My understanding is that as soon as I run "&test.doStuff" a closure is
generated. Is this correct? Would it then be valid, in doSomethingElse, to
run "GC.free(thing)" ?

Any insight would be appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110420/e0a68c77/attachment.html>


More information about the Digitalmars-d mailing list