托管(/ clr)析构函数 [英] managed (/clr) destructors

查看:118
本文介绍了托管(/ clr)析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会在下面假设''instance''被设置为''nullptr''

应该导致创建的myClass实例不再具有任何

指针指向它,因此被销毁:


ref myClass

{

public:

myClass(){}

~myClass(){} //从不调用

!myClass(){} //从不调用< br $>
};


main()

{

myClass ^ instance = gcnew myClass();

instance = nullptr; //这不应该导致破坏吗?

}


但是,如上所示,当
$ b时,析构函数或终结器都不会被调用$ b''instance''设置为''nullptr''。先前由

''实例'指出的对象是否仍然存在(.e。,它仍然有内存资源)?没有

所有指向对象的现有指针要么设置为nullptr要么出去

范围会导致对象被破坏?如果不是,我在这里缺少什么?

如果是这样,为什么析构函数或终结函数都没有被调用?


对于上下文,使用VS C ++ .NET 2005快递在/ clr模式下,我正在尝试计算

(使用静态类变量)myClass的现有实例数。

很容易计算创作(每个构造函数中的凹凸计数器),但

如何计算破坏?


提前感谢回复! :)


[== P ==]

I would assume in the following that ''instance'' being set to ''nullptr''
should cause the instance of myClass that was created to no longer have any
pointers pointing to it, and therefore be ''destroyed'':

ref myClass
{
public:
myClass() {}
~myClass() {} // never called
!myClass() {} // never called
} ;

main()
{
myClass^ instance = gcnew myClass() ;
instance = nullptr ; // shouldn''t this cause destruction?
}

However, as indicated, neither the destructor or finalizer get called when
''instance'' is set to ''nullptr''. Is the object previously pointed to by
''instance'' still around (.e., it still has memory resources)? Isn''t having
all existing pointers to an object either being set to nullptr or going out
of scope cause the object to be destructed? If not, what am I missing here?
If so, why is neither the destructor or finalizer called?

For context, using VS C++.NET 2005 Express in /clr mode, I''m trying to count
(using a static class variable) the number of existing instances of myClass.
It is easy to count the creations (bump counter in each constructor), but
how do I count the destructions?

thanks in advance for responses! : )

[==P==]

推荐答案

Peter ...当托管类不再可以从root访问它是否合格

垃圾收集。您可以强制垃圾收集器作为测试

的情况来强制调用Finalize。在您的示例中,您没有使用基于堆栈语义的堆栈,因此不会调用析构函数。我在这里写了一个artice

可能会有所帮助。

http://www.geocities.com/jeff_louie/...estructors.htm


Jeff


" Peter Oliphant"写道:
Peter... When a managed class is no longer reachable from root it is eligible
for garbage collection. You can force the garbage collector just as a test
case to force the call to Finalize. In your example you are not using stack
based semantics so the destructor will not be called. I wrote an artice here
that may be of help.

http://www.geocities.com/jeff_louie/...estructors.htm

Jeff

"Peter Oliphant" wrote:
我会在下面假设''instance''设置为''nullptr''
应该导致创建的myClass实例不再有任何指向它的指针,因此被摧毁':

ref myClass
{
public:
myClass(){}
~myClass(){} //从不调用
!myClass(){} //从不调用
};

main()
{
myClass ^ instance = gcnew myClass();
instance = nullptr; //这不应该导致破坏吗?

然而,如图所示,当
''instance''设置为''时,析构函数或终结器都不会被调用nullptr'。
''instance''之前指向的对象是否仍然存在(.e。,它仍然有内存资源)?是否所有现有指向对象的指针要么被设置为nullptr要么出去
范围会导致对象被破坏?如果不是,我在这里缺少什么?
如果是这样,为什么析构函数或终结器都没有被调用?

对于上下文,在/ clr模式下使用VS C ++ .NET 2005 Express,我'我试图计算
(使用静态类变量)myClass的现有实例的数量。
很容易计算创建(每个构造函数中的计数器),但
如何做我算一下破坏了吗?

提前感谢回复! :)

[== P ==]
I would assume in the following that ''instance'' being set to ''nullptr''
should cause the instance of myClass that was created to no longer have any
pointers pointing to it, and therefore be ''destroyed'':

ref myClass
{
public:
myClass() {}
~myClass() {} // never called
!myClass() {} // never called
} ;

main()
{
myClass^ instance = gcnew myClass() ;
instance = nullptr ; // shouldn''t this cause destruction?
}

However, as indicated, neither the destructor or finalizer get called when
''instance'' is set to ''nullptr''. Is the object previously pointed to by
''instance'' still around (.e., it still has memory resources)? Isn''t having
all existing pointers to an object either being set to nullptr or going out
of scope cause the object to be destructed? If not, what am I missing here?
If so, why is neither the destructor or finalizer called?

For context, using VS C++.NET 2005 Express in /clr mode, I''m trying to count
(using a static class variable) the number of existing instances of myClass.
It is easy to count the creations (bump counter in each constructor), but
how do I count the destructions?

thanks in advance for responses! : )

[==P==]



你好Jeff,

感谢您的回复! :)


好​​的,接下来有几个问题。我如何强制垃圾收集?不要
确切知道'堆栈语义'是什么(与我提供的代码示例相比),你能提供与我的代码相同的代码吗?是在堆栈中

语义形式?


你是说这个对象的资源先前由

''实例指向''仍然''有效''(即使没有办法从

代码到达它,因为没有指针存在),直到''垃圾

collector''决定开始垃圾收集,此时资源

可能会在需要时发布?


最大的问题如下:是有任何一个类的调用表明它已经变得无法进行垃圾收集(也就是说,没有

指针存在指向它)?也就是说,当指向对象的唯一(或最后一个)指针设置为/ b $ b到nullptr时,我会给出的示例代码中触发的内容是什么?
< br $> b $ b [== P ==]


" JAL" < JA*@discussions.microsoft.com>在消息中写道

新闻:E5 ********************************** @ microsof t.com ...
Hi Jeff,

Thanks for the response! : )

Ok, got a few questions then. How do I "force garbage collection"? Don''t
know exactly what ''stack semantics'' are (in contrast to the code sample I
provided), could you provide an equivalent to my code that IS in stack
semantic form?

Are you saying that the resources for the object previously pointed to by
''instance'' are still ''valid'' (even though there is no way to get to it from
the code since no pointers to it exist anymore) until the ''garbage
collector'' decides to start garbage collecting, at which point the resources
may be released if needed?

The big question is as follows: Is there any call to a class that indicates
that it has just become elligible for garbage collection (that is, no
pointers exist that point to it)? That is, something that would trigger in
the sample code I gave when the only (or last) pointer to the object is set
to nullptr?

[==P==]

"JAL" <JA*@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
Peter ...当无法再从root访问托管类时,它有资格收集垃圾。您可以强制垃圾收集器作为测试案例来强制调用Finalize。在您的示例中,您没有使用基于堆栈的语义,因此不会调用析构函数。我在这里写了一个artice

可能会有所帮助。

http://www.geocities.com/jeff_louie/...estructors.htm

杰夫

" Peter Oliphant写道:
Peter... When a managed class is no longer reachable from root it is
eligible
for garbage collection. You can force the garbage collector just as a test
case to force the call to Finalize. In your example you are not using
stack
based semantics so the destructor will not be called. I wrote an artice
here
that may be of help.

http://www.geocities.com/jeff_louie/...estructors.htm

Jeff

"Peter Oliphant" wrote:
我会在下面假设''instance''设置为''nullptr''
应该导致创建的myClass实例不再有指向它的任何指针,因此被摧毁:

参考myClass
{
公开:
myClass (){}
~myClass(){} //从不调用
!myClass(){} //从不调用
};

main()
{
myClass ^ instance = gcnew myClass();
instance = nullptr; //这不应该导致破坏吗?

然而,如上所述,当
''实例''时,析构函数或终结器都不会被调用
设置为''nullptr''。
''instance''之前指向的对象是否仍然存在(.e。,它仍然有内存资源)?是否
所有现有指向对象的指针要么设置为nullptr要么
取出范围会导致对象被破坏?如果没有,我在这里缺少什么?
如果是这样,为什么析构函数或终结器都没有被调用?

对于上下文,在/ clr中使用VS C ++ .NET 2005 Express模式,我正在尝试计算
(使用静态类变量)
myClass的现有实例的数量。
很容易计算创作(计数器)在每个构造函数中),但是我如何计算破坏?

提前感谢回复! :)

[== P ==]
I would assume in the following that ''instance'' being set to ''nullptr''
should cause the instance of myClass that was created to no longer have
any
pointers pointing to it, and therefore be ''destroyed'':

ref myClass
{
public:
myClass() {}
~myClass() {} // never called
!myClass() {} // never called
} ;

main()
{
myClass^ instance = gcnew myClass() ;
instance = nullptr ; // shouldn''t this cause destruction?
}

However, as indicated, neither the destructor or finalizer get called
when
''instance'' is set to ''nullptr''. Is the object previously pointed to by
''instance'' still around (.e., it still has memory resources)? Isn''t
having
all existing pointers to an object either being set to nullptr or going
out
of scope cause the object to be destructed? If not, what am I missing
here?
If so, why is neither the destructor or finalizer called?

For context, using VS C++.NET 2005 Express in /clr mode, I''m trying to
count
(using a static class variable) the number of existing instances of
myClass.
It is easy to count the creations (bump counter in each constructor), but
how do I count the destructions?

thanks in advance for responses! : )

[==P==]



Peter ....我使用GC.Collect强制垃圾收集仅用于测试。

我还在finalize中抛出一个异常,以证明最终确定是

被调用为main被退出。至于基于堆栈的语义,它看起来像这样:



RAII r; //这就是它

当一个对象无法访问时,它的方法

可以被调用,这是无效的。垃圾收集器没有引用计数,它通常只是在内存不足时从root用户搜索树,并且如果从根目录无法访问

对象那么对象就是它可以收集。


" Peter Oliphant"写道:
Peter.... I have use GC.Collect to force garbage collection for testing only.
I have also thrown an exception in finalize to document that finalize is
being called as main is exited. As for stack based semantics it would look
like this:

RAII r; // thats it

When an object is unreachable it is not valid in the sense that its methods
can be invoked. There is no reference counting by the garbage collector, it
normally simply searches the tree from root when memory runs low and if an
object is not reachable from root the object it can be collected.

"Peter Oliphant" wrote:
嗨杰夫,

感谢您的回复! :)

好的,接下来有几个问题。我如何强制垃圾收集?不要确切地知道'堆栈语义'是什么(与我提供的代码示例相比),你能提供与我的代码堆栈等价的语义吗?表单?

你是说'
''实例''之前指向的对象的资源仍然是有效的(即使没有办法达到它)来自
代码,因为没有任何指针存在,直到''垃圾收集器''决定开始垃圾收集,此时资源可以在需要时释放?

最大的问题如下:是否有一个类的调用表明它已经变得可以进行垃圾收集(也就是说,没有指向它的指针)?也就是说,当我将对象的唯一(或最后一个)指针设置为nullptr时,会触发我给出的示例代码的东西?

[== P = =]
Hi Jeff,

Thanks for the response! : )

Ok, got a few questions then. How do I "force garbage collection"? Don''t
know exactly what ''stack semantics'' are (in contrast to the code sample I
provided), could you provide an equivalent to my code that IS in stack
semantic form?

Are you saying that the resources for the object previously pointed to by
''instance'' are still ''valid'' (even though there is no way to get to it from
the code since no pointers to it exist anymore) until the ''garbage
collector'' decides to start garbage collecting, at which point the resources
may be released if needed?

The big question is as follows: Is there any call to a class that indicates
that it has just become elligible for garbage collection (that is, no
pointers exist that point to it)? That is, something that would trigger in
the sample code I gave when the only (or last) pointer to the object is set
to nullptr?

[==P==]



这篇关于托管(/ clr)析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆