我正在为从CA2006返回的IntPtr获得一个Review usage of ... (a 'IntPtr' instance) to determine whether it should be replaced with a SafeHandle or CriticalHandle ("Review usage of ... (a 'IntPtr' instance) to determine whether it should be replaced with a SafeHandle or CriticalHandle"),我知道它是一
我在代码中使用SafeHandles,并注意到它们有Close和Dispose方法。
这让我想知道SafeHandles是否是由垃圾收集器处理并在使用后处理的。
例如,如果我有这样一个句柄,那么在我使用完它之后,是否需要调用.Dispose或.Close方法。
var process Process.GetProcessesByName("notepad")[0];
var handle = process.SafeHandle;
众所周知,Dispose模式非常复杂,尤其是当我们有一个需要在不同层次上处理事物的类的层次结构时。建议的实现如下,摘自。
using System;
class BaseClass : IDisposable
{
// To detect redundant calls
private bool _disposed = false;
~BaseClass() => Dispose(false);
// Public implementation of Dispose pattern callable by consumers.
public
的文档说:
SafeHandle类包含一个终结器,它确保句柄关闭并保证运行,即使在意外的AppDomain卸载时,当主机可能不信任AppDomain状态的一致性时也是如此。
我不知道"AppDomain“意味着什么,但我不知道它应该一直运行,不是吗?
那么为什么这个代码:
class Program
{
static void Main(string[] args)
{
var c1 = new C();
var c2 = new C();
}
}
class C: SafeHandleZeroOrMinusOneIs
在关闭WPF应用程序中的SplashScreen时,我会收到以下错误:
Message: A SafeHandle or CriticalHandle of type 'BitmapHandle' failed to properly release the handle with value 0xD0052B9B. This usually indicates that the handle was released incorrectly via another means (such as extracting the handle using DangerousGetH
我有C# Windows服务类:
class MyService : ServiceBase
{
private void InitializeComponent() {
//some other code ...
SafeHandle sHandle = this.ServiceHandle; // I want to do this but this fails.
SetServiceObjectSecurity(sHandle, secInfo, binaryDescriptor);
//some more code ...
我有一个C# .NET 2.0CF项目,在这里我使用了许多本机方法。这些方法中有几种围绕着某种类型的HANDLE对象。我想使用如下的泛型抽象出句柄管理生命期:
public abstract class IHandleTraits
{
public abstract IntPtr Create();
public abstract void Release(IntPtr handle);
}
public sealed class SafeHandle<T> : IDisposable where T : IHandleTraits
{
private b
这是我第一次使用SafeHandle。
我需要调用这个需要UIntPtr的P/Invoke方法。
[DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int RegOpenKeyEx( UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult);
此UIntPtr将来自.NET的RegistryKey类。我将
我试着这样写:
UIntPtr x = (UIntPtr)intPtr;
..。但是编译器对此不太满意,返回了一个编译错误。
我需要进行转换,因为的P/Invoke签名需要UIntPtr:
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int RegOpenKeyEx(
UIntPtr hKey,
string subKey,
int ulOptions,
int samDesired,
out UIntPtr hkResult);
System.Runtime.InteropServices.SEHException was unhandled
Message=External component has thrown an exception.
Source=mscorlib
ErrorCode=-2147467259
StackTrace:
at Microsoft.Win32.Win32Native.CloseHandle(IntPtr handle)
at Microsoft.Win32.SafeHandles.SafeFileHandle.ReleaseHandle(
在使用本机Dll的C#中工作,它使用不透明句柄和内部引用计数,我有以下P/Invoke签名(都用DllImport属性修饰)
[DllImport("somedll.dll"]
public extern IntPtr getHandleOfA(IntPtr handleToB, int index); //(1)
public extern IntPtr makeNewHandleOfA(); //(2)
public extern void addRefHandleToA(IntPtr handleToA);
我没有太多的streams工作,所以我假设在我这里的某处有一个编码错误。
public static SqlBytes Compress(SqlBytes input)
{
byte[] data = (byte[])input.Value;
using (MemoryStream memstream = new MemoryStream(data))
{
using (GZipStream zipped = new GZipStream(memstream, CompressionMode.Compres
我希望通过使用c# API获得对象包含的大小。为了得到这个信息,首先我需要附加到这个过程中。为了实现这一点,我使用了DataTarget.CreateSnapshotAndAttach方法。
由于某些原因,应用程序在获得带有0x80004005错误代码的System.Runtime.InteropServices.SEHException异常后崩溃。
StackTrace:
at Microsoft.Win32.SafeNativeMethods.CloseHandle(IntPtr handle)
at Microsoft.Win32.SafeHandles.SafeProces
任何关于以下错误的想法:
System.Data.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. ---> System.ObjectDisposedException: Safe handle has been closed
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& s
在循环中第二次运行此代码(使用不同的file参数)之后,我获得了MDA:
byte[] encryptedData = File.ReadAllBytes(file); // before this line it throws, see exception below
long dataOffset;
using (var stream = new MemoryStream(encryptedData))
using (var reader = new BinaryReader(stream))
{
// ... read header information which is n
我们使用运行在Windows4.5.1上的asp.net内核作为。Kestrel用作web服务器(Kestrel版本为1.1.2)。它是按照配置的。我们还使用运行在OWIN上的SignalR。
当两个事件同时发生时,问题就会发生: Windows正在停止,Krestel收到了一些新的web请求。如果满足这些条件,则Windows服务将停止响应并挂起。
Windows无法停止本地计算机上的Topshelf.Host服务。错误1061:服务此时无法接受控制消息。
混淆码:
var port = Global.Settings.Port;
var hostBuilder = new WebH
我在一个WPF应用程序中得到了一个奇怪的AccessViolationException。由于堆栈上没有用户代码,我真的不确定如何解决这个问题。而且它每2-3天才发生一次,这增加了故障排除的复杂性。
at MS.Win32.PresentationCore.UnsafeNativeMethods+MILUnknown.Release(IntPtr)
at MS.Win32.PresentationCore.UnsafeNativeMethods+MILUnknown.ReleaseInterface(IntPtr ByRef)
at System.Windows.Media.SafeMILHa