Re: Static constructors/destructors
- From: Abhijeet Dev <msnews@xxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 01:28:56 +0530
Any static field is deleted when the process exits or is killed. Static fields go to the text segment of process memory rather than data segment. Object instances (and member fields) go to the data segment and anything in data segment must be freed as soon as process frees an object pointer. You write destructor for things in data segment, never for anything in text segment. Text segment includes instructions,class-definition, static constants etc.
If a static field is subject to change, I am not sure, but the pointer to it should be in text segment and the memory should be allocated on stack/heap depending on it's type, even in that case the pointer can not be dereferenced and so, the memory used by static fields will never be deleted in process lifetime. You can check these facts better by using a profiler.
-- Abhijeet Dev
Joe Narissi wrote:
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 wrote: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.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.
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/
.
- Follow-Ups:
- Re: Static constructors/destructors
- From: Brandon Bray [MSFT]
- Re: Static constructors/destructors
- References:
- Re: Static constructors/destructors
- From: Abhijeet Dev
- Re: Static constructors/destructors
- From: Brandon Bray [MSFT]
- Re: Static constructors/destructors
- From: Joe Narissi
- Re: Static constructors/destructors
- Prev by Date: Linker Errors with /CLR option
- Next by Date: Re: Cannot launch debugger
- Previous by thread: Re: Static constructors/destructors
- Next by thread: Re: Static constructors/destructors
- Index(es):
Relevant Pages
|