静态构造函数/析构函数 [英] Static constructors/destructors

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

问题描述

我知道如何创建和使用静态构造函数,但有没有这样的东西

作为静态析构函数?


如果没有,那怎么办你在静态

构造函数中取消分配内存?


提前谢谢,

Joe

I know how to create and use static constructors, but is there a such thing
as a static destructor?

If not, then how do you deallocate memory intialized in the static
constructor?

Thanks in advance,
Joe

推荐答案

当第一个

时间引用类时,将执行静态构造函数,进程中的任何静态数据将(/必须)保留在内存中,直到

进程被终止 - 正在运行的进程中的静态数据不能被破坏,并且当进程退出时它将永远被破坏。

.Net中的静态构造函数初始化静态数据,有可能是
因为编译的程序集只是执行本机

程序集的存根,代码是JIT编译并填充在那里。

只是尝试将这种情况与编译为原始汇编的任何语言相匹配,静态数据必须是initia一旦装配好

加载到内存中就会上传。


-

Abhijeet Dev

Joe Narissi写道:
Static constructor is executed when a class is referenced for the first
time, any static data in a process will(/must) remain in memory until
the process is killed - static data in a running process can not be
destructed and it will always be destructed when the process exits.
Static constructor in .Net initializes static data, it is possible
because compiled assembly is just a stub of the executing native
assembly and the code is JIT compiled and filled in there.
Just try to match the situation with any language which compiles to
native assembly, static data must be initialized as soon as the assembly
is loaded in memory.

--
Abhijeet Dev

Joe Narissi wrote:
我知道如何创建和使用静态构造函数,但是有没有像静态析构函数这样的东西?

如果没有,那么如何在静态
构造函数中释放初始化的内存?

提前致谢,>
I know how to create and use static constructors, but is there a such thing
as a static destructor?

If not, then how do you deallocate memory intialized in the static
constructor?

Thanks in advance,
Joe



Abhijeet Dev写道:
Abhijeet Dev wrote:
静态构造函数是在第一次引用类时执行的,进程中的任何静态数据都将(/必须)保留在内存中,直到
进程被终止 - 正在运行的进程中的静态数据无法被破坏,并且当进程退出时它将永远被破坏。
.Net中的静态构造函数初始化静态数据,这是可能的
因为编译的程序集只是执行本机程序集的存根,代码是JIT comp在那里填充和填充。
只是尝试将这种情况与编译为本机程序集的任何语言相匹配,一旦程序集
加载到内存中,就必须初始化静态数据。
Static constructor is executed when a class is referenced for the first
time, any static data in a process will(/must) remain in memory until
the process is killed - static data in a running process can not be
destructed and it will always be destructed when the process exits.
Static constructor in .Net initializes static data, it is possible
because compiled assembly is just a stub of the executing native
assembly and the code is JIT compiled and filled in there.
Just try to match the situation with any language which compiles to
native assembly, static data must be initialized as soon as the assembly
is loaded in memory.




为了增加一些特定情况的清晰度...静态构造函数是不是构造一个对象,而是为静态成员赋值

在使用之前。因此,将

静态构造函数与单个析构函数匹配并不是很有意义。


这些静态成员可能是需要销毁的类型。托管类型
分配给静态变量的
将依靠终结器清理

资源。


-

Brandon Bray,Visual C ++编译器 http://blogs.msdn.com/ branbray /

错误?建议?反馈? http://msdn.microsoft.com/productfeedback/



To add more clarity about some specific cases... the static constructor is
not constructing an object, but rather assigning values to static members
before they are used. Therefore, it doesn''t quite make sense to match a
static constructor with a single destructor.

Those static members could be types that need destruction. Managed types
assigned to a static variable will rely on the finalizer to clean up
resources.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/


我有一个c ++。NET 2003类库,它有一个具有以下基本结构的类,带有一个初始化非托管内存的静态构造函数;

主要是char * s我需要传递给非托管函数(c函数,

结构等)(我必须__pin)。


下面的char * s是否被声明为非托管,因此需要删除?


public __gc class EAHConnection:public IEAHConnection

{< br $>

受保护:

static String * m_defaultConnectionString;

static int m_defaultDnsTimeout;

static char * m_domainIsamPath ;

静态字符* m_domainDatPath;


静态EAHConnection()

{

m_defaultDnsTimeout = Int32 :: Parse(读者 - >值);

m_defaultCon nectionString = String :: Copy(reader-> Value);


m_domainIsamPath =(char *)(void *)Marshal :: StringToHGlobalAnsi(

reader - >值);

m_domainDatPath =(char *)(void *)Marshal :: StringToHGlobalAnsi(

reader-> Value);

}


}

我不是必须在某处释放这些变量吗?喜欢......


Marshal :: FreeHGlobal((int)m_domainIsamPath);

Marshal :: FreeHGlobal((int)m_domainDatPath);

非常感谢您的努力和帮助。


Joe


" Brandon Bray [MSFT]"写道:
I have a c++.NET 2003 class library that has a class with the following basic
structure, with a static constructor that initializes unmanaged memory;
mainly char*s that I need to pass to unmanaged functions (c functions,
structures, etc) (where I have to __pin).

Arent the char*s below declared unmanaged and therefore need to be deleted?

public __gc class EAHConnection : public IEAHConnection
{

protected:
static String* m_defaultConnectionString;
static int m_defaultDnsTimeout;
static char* m_domainIsamPath;
static char* m_domainDatPath;

static EAHConnection()
{
m_defaultDnsTimeout = Int32::Parse(reader->Value);
m_defaultConnectionString = String::Copy(reader->Value);

m_domainIsamPath = (char*)(void*)Marshal::StringToHGlobalAnsi(
reader->Value);
m_domainDatPath = (char*)(void*)Marshal::StringToHGlobalAnsi(
reader->Value);
}

}
Dont I have to free those variables somewhere? Like...

Marshal::FreeHGlobal((int)m_domainIsamPath);
Marshal::FreeHGlobal((int)m_domainDatPath);
Thank you very much for effort and help.

Joe

"Brandon Bray [MSFT]" wrote:
Abhijeet Dev写道:
Abhijeet Dev wrote:
静态构造函数是在第一次引用类时执行的,进程中的任何静态数据将(/必须)保留在内存中,直到进程被终止 - 正在运行的进程中的静态数据无法被破坏,并且当进程退出时它将永远被破坏。
静态构造函数.Net初始化静态数据,这是可能的
因为编译程序集只是执行本机程序集的存根,代码是JIT编译并填充在那里。
只是尝试匹配情况使用任何编译为原生程序集的语言,只要程序集在内存中加载,就必须初始化静态数据。
Static constructor is executed when a class is referenced for the first
time, any static data in a process will(/must) remain in memory until
the process is killed - static data in a running process can not be
destructed and it will always be destructed when the process exits.
Static constructor in .Net initializes static data, it is possible
because compiled assembly is just a stub of the executing native
assembly and the code is JIT compiled and filled in there.
Just try to match the situation with any language which compiles to
native assembly, static data must be initialized as soon as the assembly
is loaded in memory.



为某些内容添加更多清晰度具体情况......静态构造函数不构造对象,而是在使用静态成员之前将值赋值给
。因此,将
静态构造函数与单个析构函数匹配并不是很有意义。

这些静态成员可能是需要销毁的类型。分配给静态变量的托管类型将依赖终结器来清理资源。

- Brandon Bray,Visual C ++编译器 http://blogs.msdn.com/branbray/
错误?建议?反馈? http://msdn.microsoft.com/productfeedback/



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

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