Proposal 2: Exceptions and @nogc

Andrew Godfrey via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 9 17:12:57 PDT 2017


On Sunday, 9 April 2017 at 20:15:46 UTC, Walter Bright wrote:
> On 4/9/2017 1:35 AM, Dukc wrote:
>> object aMemoryLeak;
>>
>> void someFunc()
>> {   throw (aMemoryLeak = new Exception("hello world!"));
>> }
>>
>> Would the compiler warn about this or make the exception 
>> normally garbage
>> collected?
>
> That would be a regular gc allocated Exception.

Iternally, would we create a temp Exception with _refcount = 2, 
and then on assignment to 'aMemoryLeak', change the refcount to 0?

I understand that the compiler would probably optimize that away 
in this case, but it seems that's a general answer that would 
work.

So in that case:
1. Do you really need the "_refcount=1" state as currently 
defined? I'd think the only code which sees this state, has just 
decremented it from 2 and is about to delete it.

2. Echoing others here: This seems like a general model D could 
use, where you have refcounted objects, but at any time, a gc 
reference could be taken, which is indicated by setting the 
refcount to 0 but leaving the object alive. Is it general? If 
not, what is special about Exceptions that makes it work here?


More information about the Digitalmars-d mailing list