c# - What is the cause of this OutOfMemoryException on Mutex constructor? -


i getting system.outofmemoryexception on line of code:

mutex2 = new mutex(true, "name2"); 

here stacktrace:

{"exception of type 'system.outofmemoryexception' thrown."}    @ microsoft.win32.win32native.createmutex(security_attributes lpsecurityattributes, boolean initialowner, string name)    @ system.threading.mutex.createmutexhandle(boolean initiallyowned, string name, security_attributes securityattribute, safewaithandle& mutexhandle)    @ system.threading.mutex.mutextrycodehelper.mutextrycode(object userdata)    @ system.runtime.compilerservices.runtimehelpers.executecodewithguaranteedcleanup(trycode code, cleanupcode backoutcode, object userdata)    @ system.threading.mutex.createmutexwithguaranteedcleanup(boolean initiallyowned, string name, boolean& creatednew, security_attributes secattrs)    @ system.threading.mutex..ctor(boolean initiallyowned, string name, boolean& creatednew, mutexsecurity mutexsecurity)    @ system.threading.mutex..ctor(boolean initiallyowned, string name)    @ foo.foodefinitions.foomanager.fooform.fooform_load(object sender, eventargs e) in c:\tfs\dws\trunk\dev\foodefinitions\foomanager\fooform.cs:line 92 

it occur when use impersonation. without impersonation (running on normal windows-account) run fine. impersonation this:

    if (!nativemethods.logonuser(username, domainname, password, 2, 0, ref this._tokenhandle)) // [dllimport("advapi32.dll", setlasterror = true, charset = charset.auto)]     {         throw new win32exception(marshal.getlastwin32error());     }     this._impersonateduser = new windowsidentity(this._tokenhandle).impersonate(); 

edit: eloborate, creating automated tests on legacy code. have removed use of mutexes if could. investigating securitycriticalattribute on mutex constructor.

edit2: here full example of code:

using microsoft.visualstudio.testtools.unittesting; using system; using system.componentmodel; using system.net; using system.runtime.interopservices; using system.security.principal; using system.threading;  namespace reinierdg.mutextesting {     [testclass]     public class mutextest     {         [testmethod]         public void createmutexunderimpersonation()         {             var credentials = new networkcredential("testagent", "secretpassword");             var tokenhandle = new intptr();             if (!nativemethods.logonuser(credentials.username, credentials.domain, credentials.password, 2, 0, ref tokenhandle))             {                 throw new win32exception(marshal.getlastwin32error());             }             var impersonateduser = new windowsidentity(tokenhandle).impersonate();             // run indefinately or untill memory full 1 cpu core @ 100%             var mutex = new mutex(true, "test");         }          internal static class nativemethods         {             [dllimport("advapi32.dll", setlasterror = true, charset = charset.auto)]             internal static extern bool logonuser([marshalas(unmanagedtype.lpwstr)]string lpszusername, [marshalas(unmanagedtype.lpwstr)]string lpszdomain, [marshalas(unmanagedtype.lpwstr)]string lpszpassword, int dwlogontype, int dwlogonprovider, ref intptr phtoken);         }     } } 

    // run indefinately or untill memory full 

well, 1 explanation. we'll have assume comment doesn't match code. obvious issue here did not post stack trace relevant enough error , not diagnose underlying problem. can post hints next stage.

it easy assume createmutex() failed. not case, failure of winapi function reported differently, you'd see __error.winioerror() in stack trace. , error code different, you'd error 1450, error_no_system_resources, "insufficient system resources exist complete requested service".

it in fact clr threw exception. or in other words, pinvoke marshaller failed. considerably complicates diagnostic, there large number of places in large amount of code can throw oom. needs allocate unmanaged memory pinvoke job done, if fails oom-kaboom. many ways can happen, internal unmanaged heap corruption enough. logonuser() pinvoke declaration technically wrong (charset.auto != unmanagedtype.lpwstr), not wrong enough explain problem.

you'll need closer root of exception , requires enabling unmanaged debugger. vs2015, use project > properties > debugging tab > tick "enable native code debugging" checkbox. you'll need debugging symbols clr make sense of stack trace. use tools > options > debugging > symbols > tick "microsoft symbol server". need make debugger stop on first-chance exception, use debug > windows > exception settings > tick "win32 exceptions".

you'll know lot more now, can post better stack trace in question. still, odds going give users or crystal-clear diagnostic shows how mishap explained impersonation remote. calling in of microsoft support wise, going need know lot more how "testagent" account configured. keep in mind such accounts intentionally crippled ensure unit tests can't demand many system resources.


Comments