简体   繁体   English

确定对象是否已被破坏的惯用方式

[英]Idiomatic way to determine if an object has been destroyed

I've been trying to find a better way to accomplish determining if a particular object has been destroyed ( destroy(...) ). 我一直在尝试找到一种更好的方法来完成确定某个特定对象是否已被破坏的方法( destroy(...) )。 The way I've been doing it is like so: 我一直这样做的方式是这样的:

class C { 
  bool valid = false; 
  this(){
    valid = true; 
  } 
}

Then you do: 然后,您执行以下操作:

C c = new C;
c.valid.writeln // true
destroy(c);
c.valid.writeln // false
if(c !is null && !c.valid) c = null;

I don't see anything wrong with this (perhaps someone can tell me something else wrong with it) other than that it takes up memory and requires putting valid = true; 除了占用内存并需要将valid = true;占用外,我看不到任何其他错误(也许有人可以告诉我其他错误) valid = true; in each constructor (and is ugly because it uses a variable from a destroyed object). 在每个构造函数中(这很丑陋,因为它使用了来自销毁对象的变量)。 The best case, of course, would be to have some magical function that can just tell you if some object is valid or not valid(c); // true / false 当然,最好的情况是拥有一些神奇的功能,可以告诉您某个对象有效还是valid(c); // true / false valid(c); // true / false . valid(c); // true / false

So my question is if there is some standard way to determine if the object has been destroyed (like, the gc hasn't collected that memory location and a valid object is sitting in that spot without a reference to a vtable) and that the pointer is now virtually dangling? 所以我的问题是,是否有某种标准的方法来确定对象是否已被销毁(例如,gc尚未收集该内存位置,并且一个有效的对象位于该位置而没有引用vtable),并且该指针现在几乎在晃来晃去吗? If there isn't any good way to do this then as a secondary question: is this method dangerous in any foreseeable way? 如果没有任何好的方法可以做到这一点,那么作为第二个问题:以任何可预见的方式,这种方法是否危险?

Previously, I made sure that for each reference from object A -> B there was a reference B -> A, and upon applying destroy A's destructor nullified B's reference to A. So I never even had to check if A was destroyed. 以前,我确保对于对象A-> B的每个引用都有一个引用B-> A,并且在应用destroy A的析构函数时,B对A的引用也无效。因此,我什至不必检查A是否被销毁。 But this is very tedious and time consuming when you want to add a new type of reference because you have to modify both the destroyable class (A) and the referencing class (B). 但是,当您要添加新的引用类型时,这非常繁琐且耗时,因为必须同时修改可销毁类(A)和引用类(B)。 Theoretically, this is something like always having a determinable cycle in the reference graph of your program (or something like that); 从理论上讲,这就像在程序的引用图中始终具有可确定的周期(或类似的东西); it is potentially a very interesting subject. 这可能是一个非常有趣的主题。

Sorry in advance if I'm being an idiot. 对不起,如果我是白痴。

By default D will use GC to deal with reference types (class in your case). 默认情况下,D将使用GC处理引用类型(在您的情况下为类)。 This means that if you use defaults, you can't expect deterministic object destruction. 这意味着,如果您使用默认值,则无法期望确定性对象销毁。

Jonathan explained that nicely in this thread: Usage preference between a struct and a class in D language Jonathan在该线程中很好地解释了这一点: D语言中的结构和类之间的用法首选项

If you really need deterministic destruction - use structs. 如果您确实需要确定性销毁,请使用结构。 The method you described reminds me of Scala's Option type. 您描述的方法让我想起了Scala的Option类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UE4:我如何检测蓝图中的演员参考是否已被破坏? - UE4: How can i detect if an actor refrence has been destroyed in blueprints? 对象在向上转换后是否可以从先前的引用访问(在 Java 中)? - Is an object accessible from a previous reference after it has been upcast (in Java)? 当“使用严格”时如何引用对象的实例; 已经申报了吗? - How to Reference an instance of an object when “use strict”; has been declared? 当引用返回的对象被销毁时? - When a object returned by reference is destroyed? 如何确定对象或引用是否具有有效的字符串强制? - How can I determine if an object or reference has a valid string coercion? “ this ==&x”是确定指针(this)和引用(x)是否指向同一对象的正确方法吗? - Is `this == &x` the correct way to determine if a pointer (this) and a reference (x) point to the same object? 用于可空引用的C ++惯用方法 - C++ idiomatic way for nullable reference 过滤数组和中断引用的简洁、惯用的方法? - Concise, idiomatic way to filter an array and break references? 拥有可以指向或引用已在堆栈上分配的不同类型数据的映射的现代方法 - Modern way to have a map that can point-to or reference data of different types that has been allocated on the stack 如何将 Option&lt;&amp;T&gt; 转换为 Option<T> 以 Rust 中最惯用的方式? - How to convert Option<&T> to Option<T> in the most idiomatic way in Rust?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM