RAII who?

Jan Hanselaer jan.hanselaer at gmail.com
Sat May 19 05:09:00 PDT 2007


Thanks for the explanation Daniel en Lutger.

Some new qestions come to my mind. Since now there is the 'scope' attribute, 
the auto attribute only exists for type inference?

I have a background in Java and consequently not used to work with 
destructors. But in D apparently it is used although there is a garbage 
collector.
Are there any (style) rules that say when the scope attribute (and a 
destructor) is needed?
I would expect the garbage collector cleaning up all the resources when an 
object gets out of scope ... So till now I never used a destructor and/or 
the scope attribute.

Jan


"Lutger" <lutger.blijdestijn at gmail.com> schreef in bericht 
news:f2mo2h$22e$1 at digitalmars.com...
> Jan Hanselaer wrote:
>> Hi
>>
>> I'm confused about what's meant with Resource Acquisition Is 
>> Initialisation. Can somebody explain this, especially how it is 
>> accomplished in D?
>> Some questions that  arise in my brain when reading and thinking about 
>> it.
>>
>> - Giving each variable a default value is this part of RAII? Or has it 
>> nothing to do with it?
>> - What's the use of the keyword 'auto' in the whole story?
>> - What's the link with garbage collection?
>> - If I don't use the keyword auto, memory resources are not automatically 
>> freed?
>> - ...
>>
>> I just don't seem to get te whole picture here. But I suspect it's not 
>> that complicated.
>> Anyone with a short and clear definition?
>>
>> Thanks in advance!
>> Jan
>
> According to wikipedia: "The acquisition is bound to the construction 
> (initialization) whereas the release is bound to the automatic, 
> deterministic destruction (uninitialization) of the variable. Since a 
> destructor of an automatic variable is called when leaving its scope, it 
> can be guaranteed that the resource is released as soon as the variable's 
> life time ends."
>
> Thus, if we are talking about memory as a resource, this usually means for 
> every new in the constructor a delete in the destructor. The link with 
> garbage collection is that it is an alternative technique often used in 
> C++, to make it easier and safer to manage memory. Less so in D, since 
> there is the garbage collector.
>
> Auto used to be what scope classes are now. In D, contrary to C++, 
> destructors are not automatically called upon scope exit. With scope 
> classses they are, so these are more useful for RAII, for example file 
> handles. 





More information about the Digitalmars-d mailing list