Re: Static constructors/destructors

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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:
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.
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/




.



Relevant Pages

  • Re: multithreading : two easy questions
    ... a modification of a static field is considered a side effect. ... The important thing in there is "within a single thread of execution". ... to main memory until the next write memory barrier, ... > using lock statements. ...
    (microsoft.public.dotnet.general)
  • Re: very high memory usage in -current
    ... Maybe it's related to jemalloc, but I'd be surprised if no one else has noticed this before. ... The application would allocate some large chunks of memory for image ... These data would be allocated in the data segment. ...
    (freebsd-current)
  • Re: Kind of new: function implementation questions, MASM
    ... In 32-bit programming, you should simply ignore these concepts. ... A stack is just a piece of memory. ... >indicate it's a register definition. ... >the data segment to be a block of memory that I can access. ...
    (comp.lang.asm.x86)
  • Complete list of process attributes?
    ... memory, shared segments ... memory, shared segments (SysV) ... memory, data segment, arguments ... semaphores, memory ...
    (comp.unix.programmer)
  • Re: doubt in protected mode
    ... I have doubt about the memory manager. ... When i compile any asm ... zero-out CS/DS in 32-bit Protected Mode, ... protected or how to know whether the content of code and data segment ...
    (alt.lang.asm)