digitalmars.D.learn - is there any way for an object to make it self no longer usable
- Sean Campbell (22/22) Jul 19 2014 is there any way for an object to make it self no longer usable?
- Kagamin (2/2) Jul 19 2014 You can encapsulate it in a wrapper, which will control access to
- Sean Campbell (4/6) Jul 19 2014 how ?
- Frustrated (13/35) Jul 19 2014 You do realize that if you do this then anywhere in your code
- Kagamin (5/7) Jul 19 2014 Of course, the object should not be used after destruction. The
is there any way for an object to make it self no longer usable? eg class someclass { string somevalue; bool someflag; int somelength; this (value,length,flag) { somevalue = value; someflag = flag; somelength = length; } void modify_value(string new_value){ somevalue = new_value; } void finalize_data(){ //do something with data //make this invalid // eg delete this or this = null } } I don't want to use a private flag to tell weather the object is valid or not
Jul 19 2014
You can encapsulate it in a wrapper, which will control access to the object and reject if necessary.
Jul 19 2014
On Saturday, 19 July 2014 at 13:46:08 UTC, Kagamin wrote:You can encapsulate it in a wrapper, which will control access to the object and reject if necessary.how ? surely not by writing another class with wrapper methods for the existing one.
Jul 19 2014
On Saturday, 19 July 2014 at 17:05:27 UTC, Sean Campbell wrote:surely not by writing another class with wrapper methods for the existing one.If you don't want to write the class, you can write a generator for it. See BlackHole wrapper for an example.
Jul 19 2014
On Saturday, 19 July 2014 at 12:02:50 UTC, Sean Campbell wrote:is there any way for an object to make it self no longer usable? eg class someclass { string somevalue; bool someflag; int somelength; this (value,length,flag) { somevalue = value; someflag = flag; somelength = length; } void modify_value(string new_value){ somevalue = new_value; } void finalize_data(){ //do something with data //make this invalid // eg delete this or this = null } } I don't want to use a private flag to tell weather the object is valid or notYou do realize that if you do this then anywhere in your code you'll have to check if the object is valid before use? If the objects lifetime is "random"(because the object itself can decide when to destroy itself), then there is no way to know when it will be destroyed. If you do this you are potentially asking for a lot of access violation errors or undefined behavior. In any case, an easy way is to allow the object to allocate and deallocate itself. If the object knows the ptr and size that was used to allocate itself it is trivial to deallocate itself. http://dlang.org/phobos/core_memory.html
Jul 19 2014
On Saturday, 19 July 2014 at 17:28:13 UTC, Frustrated wrote:If you do this you are potentially asking for a lot of access violation errors or undefined behavior.Of course, the object should not be used after destruction. The trick is to reliably diagnose it. If such uses are not intercepted, they may remain unnoticed. GC can't solve the issue, because it's not a memory resource, which is released.
Jul 19 2014