I have made a discovery

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 23 11:29:27 PDT 2015


On Saturday, 18 April 2015 at 15:24:27 UTC, w0rp wrote:
> The following code almost compiles.
>
> --------
> import core.stdc.stdlib;
>
> class Foo : Exception {
>     @nogc pure nothrow @safe
>     this(string msg, string file = __FILE__, size_t line = 
> __LINE__, Throwable next = null) {
>         super(msg, file, line, next);
>     }
>
>     @nogc
>     new(size_t size) {
>         return malloc(size);
>     }
> }
>
> @nogc
> void main() {
>     throw new Foo("Oh no!");
> }
> --------
>
> That's right. An unofficially deprecated feature of the 
> language and a newer feature of the language coming together in 
> an interesting way. The only thing stopping this code from 
> actually working is a trivial change to druntime to mark the 
> Throwable, Exception, and Error constructors as @nogc, which I 
> just created a pull request for directly through GitHub.
>
> https://github.com/D-Programming-Language/druntime/pull/1223
>
> Now imagine that instead of just a malloc which leaks memory 
> like the above, other allocation schemes are used here instead. 
> Consider also the coming addition to the language for class 
> reference counting methods opAddRef and opRelease. Then let 
> your imagination run wild.
>
> Enjoy!

I don't see anything interesting here. Controlling allocation was 
never the issue (MyException.create ftw). It is deallocation that 
matters.


More information about the Digitalmars-d mailing list