Preferred method of creating objects, structs, and arrays with deterministic memory management

Guillaume Piolat via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 1 05:20:11 PDT 2016


On Wednesday, 1 June 2016 at 08:53:01 UTC, Rene Zwanenburg wrote:
>> I was wondering: what's the preferred method for deterministic 
>> memory management?
>
>
> You can annotate your functions as @nogc. The compiler will 
> disallow any potential GC use, including calling other 
> functions that are not @nogc.
>
> P0nce is doing real time audio stuff, iirc he's using a thread 
> with a @nogc entry point for the strictly real-time parts, and 
> regular GC-using code in another thread for everything else.

Yes, GC is a minor concern for me. I'm happy to use the GC in UI 
stuff next to the @nogc part. As tired as the expression is, it's 
a "best of both world" situation.

I tried GC.disable(), it was bringing lots of stability problems 
because not all my code was @nogc and rogue allocations occured. 
I guess I deserved it?

I'm a heavy user of the GC-proof resource class "idiom" which 
turn your turn the GC into a leak detector: 
https://p0nce.github.io/d-idioms/#GC-proof-resource-class
That way you can make polymorphic resources with relative safety.
Exception safety is harder though for class objects. Usually 
struct should be used.

I'd say D has a more complex story for resources, but it's not 
necessary a bad story. When writing a writeln("hello world"); I 
don't really want to think about who owns the string "hello 
world". That's what affine types are about, one size fits all.




More information about the Digitalmars-d-learn mailing list