Ich erhalte eine System.OutOfMemoryException
auf dieser Codezeile:Was ist die Ursache für diese OutOfMemoryException beim Mutex-Konstruktor?
mutex2 = new Mutex(true, "Name2");
Hier ist die Stacktrace:
{"Exception of type 'System.OutOfMemoryException' was thrown."}
at Microsoft.Win32.Win32Native.CreateMutex(SECURITY_ATTRIBUTES lpSecurityAttributes, Boolean initialOwner, String name)
at System.Threading.Mutex.CreateMutexHandle(Boolean initiallyOwned, String name, SECURITY_ATTRIBUTES securityAttribute, SafeWaitHandle& mutexHandle)
at System.Threading.Mutex.MutexTryCodeHelper.MutexTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.Mutex.CreateMutexWithGuaranteedCleanup(Boolean initiallyOwned, String name, Boolean& createdNew, SECURITY_ATTRIBUTES secAttrs)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
at Foo.FooDefinitions.FooManager.FooForm.FooForm_Load(Object sender, EventArgs e) in c:\tfs\DWS\TRUNK\DEV\FooDefinitions\FooManager\FooForm.cs:line 92
Es tritt nur dann auf, wenn ich den Identitätswechsel verwenden. Ohne Identitätswechsel (läuft auf meinem normalen Windows-Account) läuft es gut. Der Identitätswechsel ist so etwas wie dieses:
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: Nur um eloborate, ich schaffe automatisierte Tests auf Legacy-Code. Ich hätte die Verwendung von Mutexen entfernt, wenn ich könnte. Ich untersuche gerade die SecurityCriticalAttribute
auf dem Mutex-Konstruktor.
EDIT2: Hier ist ein vollständiges Beispiel des Codes:
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();
// this will run indefinately or untill memory is full with 1 cpu core at 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);
}
}
}
[hier] (http://referencesource.microsoft.com/#mscorlib/system/threading/mutex.cs,434) ist die Quelle. Es zeigt "CreateMutex" in einem 'while (true)'; Vielleicht hast du einen Endlosschleife Bug gefunden? – Quantic
Code Ihrer 'FooForm.FooForm_Load' Methode – giammin
aber vor allem: Sind Sie sicher, dass Sie einen Mutex brauchen? in einem Formular laden Ereignis ??? – giammin