|
This post originated from an RSS feed registered with .NET Buzz
by Eric Gunnerson.
|
Original Post: Poser: What is the lifetime of local instances?
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
|
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium
|
|
A reader wote me to ask a question about variable lifetimes. Consider the following code:
class Mutex
{
public Mutex(string name)
{
hMutex = Kernel32.CreateMutex(null,false,name);
Kernel32.WaitForSingleObject(hMutex,0);
}
~Mutex()
{
Kernel32.ReleaseMutex(hMutex);
}
}
class MyClass
{
void MyFunc()
{
Mutex m = new Mutex("MyGlobalMutex");
// (some code here which accesses, or calls functions which access, a shared resource...
// Note: no explicit reference to m, especially no call to destroy or clean up m!)
}
}
Will the mutex will be held for the duration of method MyFunc()?
Answer to follow in the next post
Read: Poser: What is the lifetime of local instances?