析构函数和垃圾收集器 [英] Destructor and garbagecollector

查看:60
本文介绍了析构函数和垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个dotnet类,在其构造函数中从中央许可证池中消耗许可证

。在析构函数中它再次释放许可证。

但是因为我不知道析构函数何时被调用(这取决于

垃圾收集器)对象(因此应用程序)使用许可证

虽然它已不再使用了。


我当然可以写一个特定的注销消息。运行特定部分的方法

释放许可证的析构函数代码,但是我必须处理

一个去初始化的对象。因此不可用。


有没有其他地方可以实现析构函数代码,在删除对象时会立即调用




彼得

解决方案




查看Dispose模式(Google对于Dispose pattern MSDN,没有

引号,你应该找到它)。没有办法获得与

C ++堆栈对象相同的行为,但Dispose是获取内容的公认方式

及时发布资源。


史蒂夫


" Peter Hemmingsen" < ph*@softco.dk>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...



我编写了一个dotnet类,它在构造函数中从中央许可证池中获取许可证。在析构函数中它再次释放许可证。
但是因为我不知道什么时候调用析构函数(这取决于
垃圾收集器)对象(以及应用程序)消耗
许可证虽然它已不再使用了。

我当然可以写一个特定的注销。运行释放许可证的析构函数代码的特定
部分的方法,但是我必须使用去初始化的对象处理
。因此不可用。

还有其他地方可以实现析构函数代码,当对象被删除时,
会立即被调用吗?

彼得



你好Steve,


感谢您的回复。我想这就是我必须这样做的方式。但是后来我需要在所有方法(和属性)中添加代码来检查是否已经调用了Dispose

, - 如果是,则抛出异常。关于

析构函数的好处是该对象不再存在,因此方法无法调用



彼得


" Steve McLellan" < sjm.NOSPAM AT fixerlabs DOT com>在消息中写道

新闻:Ot ************* @ tk2msftngp13.phx.gbl ...



查看Dispose模式(Google forDispose pattern MSDN,不带
引号,您应该找到它)。没有办法获得与C ++堆栈对象相同的行为
,但是Dispose是获取内容以及时释放资源的公认方式。

Steve

Peter Hemmingsen < ph*@softco.dk>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...



我编写了一个dotnet类,它在构造函数中从中央许可证池中获取许可证。在析构函数中,它再次释放许可证
。但是因为我不知道什么时候调用析构函数(这取决于
垃圾收集器)对象(以及应用程序)使用


许可证

虽然它不再使用了。

我当然可以写一个特定的注销。运行释放许可证的析构函数代码的特定


部分

的方法,但是我必须用


post_quotes>去初始化的对象。因此不可用。

当对象被删除时,是否还有其他地方可以立即实现析构函数代码


? br />
彼得







Dispose模式可以帮助你解决这个问题 - 多次调用它可以安全地多次调用它b / b
。只要你知道你什么时候完成它就会产生什么效果。顺便说一句,有一些东西只是突然间没有它是不是很好的方式来完成它 - 如果你有一些东西

存储指向对象的指针和指向的东西还没有,

你会得到空指针异常。听起来你可能想要使用

类似弱指针的东西(或弱引用,.NET东西调用

)。我从来没有在MC ++中使用它们,但要点是弱指针将不会让GC保持对象的活动,但如果对象是

仍然活着,你尝试从弱指针使用它,它将创建一个

强指针供你使用。如果你使用的是非托管C ++,那么Boost

库就会有很大的影响力。否则请查看.NET文档。对于

''弱引用''。


此外,您可能需要考虑某种消息系统让

容器知道它应该存储的东西不存在任何

更多。也许你可以在这里发布简化代码,这样我就能更好地了解你在做什么?


史蒂夫

" Peter Hemmingsen" < ph*@softco.dk>在消息中写道

news:uN ************** @ tk2msftngp13.phx.gbl ...

嗨Steve,

感谢您的回复。我想这就是我必须这样做的方式。但是我需要在所有方法(和属性)中添加代码来检查是否已经调用
Dispose, - 如果是,则抛出异常。关于
析构函数的好处是对象根本不再存在,所以方法可以
不被调用。

彼得

史蒂夫麦克莱伦" < sjm.NOSPAM AT fixerlabs DOT com>在消息中写道
新闻:Ot ************* @ tk2msftngp13.phx.gbl ...



查看Dispose模式(Google forDispose pattern MSDN,不带

引号,你应该找到它)。没有办法获得与

C ++堆栈对象相同的
行为,但Dispose是获取内容以及时释放资源的公认方式。

史蒂夫

Peter Hemmingsen < ph*@softco.dk>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...



我编写了一个dotnet类,在其构造函数中从中央许可证池中获取
许可证。在析构函数中,它再次释放许可证


。但是因为我不知道什么时候调用析构函数(最多
垃圾收集器)对象(以及应用程序)使用


许可

虽然它不再使用了。

我当然可以写一个特定的注销。运行释放许可证的析构函数代码的特定


部分

的方法,但是我必须用


post_quotes>去初始化的对象。因此不可用。

当对象被删除时,是否还有其他地方可以立即实现析构函数代码


? br />
彼得





Hi,

I have written a dotnet class that in its constructor consumes a license
from a central license pool. In the destructor it free the license again.
But since I don''t know when the destructor is called (that is up to the
garbage collector) the object (and hence the application) consume a license
although it is not in use any more.

I could of course write a specific "logoff" method to run the specific part
of the destructor code that frees the license, but then I have to deal with
an object that is "de-initialized" and therefore not usable.

Is there any other place to implement destructor code that are always called
immediately when the object is deleted?

Peter

解决方案

Hi,

Check out the Dispose pattern (Google for "Dispose pattern MSDN" without the
quotes and you should find it). There is no way to get the same behaviour as
C++ stack objects, but Dispose is the recognized way of getting stuff to
release resources in a timely fashion.

Steve

"Peter Hemmingsen" <ph*@softco.dk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hi,

I have written a dotnet class that in its constructor consumes a license
from a central license pool. In the destructor it free the license again.
But since I don''t know when the destructor is called (that is up to the
garbage collector) the object (and hence the application) consume a license although it is not in use any more.

I could of course write a specific "logoff" method to run the specific part of the destructor code that frees the license, but then I have to deal with an object that is "de-initialized" and therefore not usable.

Is there any other place to implement destructor code that are always called immediately when the object is deleted?

Peter



Hi Steve,

Thanks for your reply. I guess that is the way I have to do it. But then I
need to add code to all the method (and properties) to check if the Dispose
has been called,- and if so throw an exception. The nice thing about the
destructor is that the object simply no longer exist so the methods can not
be called.

Peter

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:Ot*************@tk2msftngp13.phx.gbl...

Hi,

Check out the Dispose pattern (Google for "Dispose pattern MSDN" without the quotes and you should find it). There is no way to get the same behaviour as C++ stack objects, but Dispose is the recognized way of getting stuff to
release resources in a timely fashion.

Steve

"Peter Hemmingsen" <ph*@softco.dk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hi,

I have written a dotnet class that in its constructor consumes a license
from a central license pool. In the destructor it free the license again. But since I don''t know when the destructor is called (that is up to the
garbage collector) the object (and hence the application) consume a


license

although it is not in use any more.

I could of course write a specific "logoff" method to run the specific


part

of the destructor code that frees the license, but then I have to deal


with

an object that is "de-initialized" and therefore not usable.

Is there any other place to implement destructor code that are always


called

immediately when the object is deleted?

Peter




Hi,

The Dispose pattern helps you cope with that - it should be safe to call it
multiple times without a problem. As long as you know when you''re finished
with something it should work. Incidentally, having something just suddenly
not be there isn''t a great way to be done with it - if you have something
storing pointers to objects and the things pointed to aren''t there any more,
you''ll get null pointer exceptions. It sounds like you might want to use
something similar to a weak pointer (or weak reference, the .NET stuff calls
it). I''ve never used them in MC++ but the gist is that weak pointers will
not keep an object alive as far as the GC is concerned, but if the object is
still alive and you try to use it from the weak pointer it will create a
strong pointer for you to use. If you''re using unmanaged C++ the Boost
library has a great imlpementation; otherwise check out the .NET docs. for
''weak references''.

Additionally, you may want to consider some kind of messaging system to let
the container know that something it should be storing doesn''t exist any
more. Maybe you can post simplified code here so I can get a better idea of
what you''re doing?

Steve

"Peter Hemmingsen" <ph*@softco.dk> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...

Hi Steve,

Thanks for your reply. I guess that is the way I have to do it. But then I
need to add code to all the method (and properties) to check if the Dispose has been called,- and if so throw an exception. The nice thing about the
destructor is that the object simply no longer exist so the methods can not be called.

Peter

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:Ot*************@tk2msftngp13.phx.gbl...

Hi,

Check out the Dispose pattern (Google for "Dispose pattern MSDN" without the

quotes and you should find it). There is no way to get the same behaviour as

C++ stack objects, but Dispose is the recognized way of getting stuff to
release resources in a timely fashion.

Steve

"Peter Hemmingsen" <ph*@softco.dk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hi,

I have written a dotnet class that in its constructor consumes a license from a central license pool. In the destructor it free the license


again. But since I don''t know when the destructor is called (that is up to the garbage collector) the object (and hence the application) consume a


license

although it is not in use any more.

I could of course write a specific "logoff" method to run the specific


part

of the destructor code that frees the license, but then I have to deal


with

an object that is "de-initialized" and therefore not usable.

Is there any other place to implement destructor code that are always


called

immediately when the object is deleted?

Peter





这篇关于析构函数和垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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