Questions tagged [safehandle]

28 questions
7
votes
4 answers

ObjectDisposedException: Safe handle has been closed

So this is a rather small question with a big explanation. As is noted by the title I am getting an unhandled exception telling me my Safe handle has been closed. What I'll probably have to do is edit this post a few times with more and more code to…
Robert Snyder
  • 2,399
  • 4
  • 33
  • 65
3
votes
1 answer

SafeHandle with memory pressure

The SafeHandle class allows the safe access to native resources under .NET with reference counting and cooperation from the P/Invoke marshaller to avoid premature disposal of handles that could lead to an application crash. In some situation, it…
3
votes
1 answer

ObjectDisposedException - DangerousAddRef

I got a somehow complex .net console application, that uses WPF for some notification windows and also does some http calls. In very rare cases, that application crashes and the only error message I'm able to get comes from windows event…
DanielG
  • 1,217
  • 1
  • 16
  • 37
3
votes
2 answers

error: a SafeHandle or CriticalHandle of type ZLibStreamHandle failed to properly release

I haven't worked much with streams, so I assume there's a coding error here on my part, somewhere. public static SqlBytes Compress(SqlBytes input) { byte[] data = (byte[])input.Value; using (MemoryStream memstream = new…
Tim
  • 8,669
  • 31
  • 105
  • 183
3
votes
1 answer

Constraints vs abstract class using SafeHandle

There is a method within BCryptNative called GetInt32Property. It has the following signature: internal static int GetInt32Property(T algorithm, string property) where T : SafeHandle This method only works when T is of type…
hl3mukkel
  • 5,369
  • 3
  • 21
  • 21
3
votes
1 answer

Working with SafeHandles in F#

I'm working with some F# code that uses platform invoke. One of the APIs I'm using returns a handle. Instead of using a nativeint, I've implemented my own SafeHandle (specifically SafeHandleMinusOneIsInvalid.) This makes working with the module…
vcsjones
  • 138,677
  • 31
  • 291
  • 286
3
votes
1 answer

After using FileStream.SafeFileHandle, is it safe for me to remove a call to GC.KeepAlive()?

Consider the following code which I recently changed to use FileStream.SafeFileHandle: public static void FastWrite(FileStream fs, T[] array, int offset, int count) where T: struct { int sizeOfT = Marshal.SizeOf(typeof(T)); GCHandle…
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
2
votes
0 answers

SafeHandle vs IntPtr in WinAPIs and CloseHandle in C#

I read many articles about SafeHandle and IDiposable but I still don't understand whether I should use SafeHandle and CloseHandle or not in C#. MSDN SafeHandle example IntPtr, SafeHandle and HandleRef -…
nop
  • 4,711
  • 6
  • 32
  • 93
2
votes
1 answer

Which `IntPtr` flavors are candidates for a `SafeHandle`

I am getting a CA2006 ("Review usage of ... (a 'IntPtr' instance) to determine whether it should be replaced with a SafeHandle or CriticalHandle") for an IntPtr returned from ConvertStringSecurityDescriptorToSecurityDescriptor which I know is a…
Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
2
votes
1 answer

Safe Handles when marshaling Win32 structs (PROCESS_INFORMATION)

I am in the process of converting my Win32 p/invoke code to use SafeHandle classes instead of the typical IntPtr handles. While everything works pretty well in DllImport method signatures, I cannot for the life of me get them to work when marshaling…
Erik
  • 12,730
  • 5
  • 36
  • 42
2
votes
2 answers

What's the purpose of invalidHandleValue in SafeHandle?

The SafeHandle constructor takes an invalidHandleValue. What is it used for if you have to implement IsInvalid anyway because it has no idea which member variable holds the pointer [I didn't know it implemented the handle member variable for you]?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
1
vote
0 answers

System.ObjectDisposedException: Safe handle has been closed occurs when I try to open SQL Teradata connection using TdConnection class

I have a C# console application trying to connect to SQL Teradata using TdConnection class. Below is my code: using (TdConnection conn = new TdConnection(connstr)) { using (TdCommand tdCmd = new TdCommand(selectQry)) { using…
1
vote
2 answers

How to get creation DateTime of file from very long path?

I have file paths which are very long, so can be handled only using SafeFileHandle. Want to get creation datetime. When tried getting millies and then converted in DateTime then it is 1600 years less. Code: [DllImport("kernel32.dll", CharSet =…
Prem
  • 316
  • 1
  • 5
  • 23
1
vote
1 answer

Cannot pass SafeHandle instance in ReleaseHandle to native method

I just recently learned about SafeHandle and, for a test, I implemented it for the SDL2 library, creating and destroying a window: [DllImport(_libName, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr SDL_CreateWindow( …
Ray
  • 7,940
  • 7
  • 58
  • 90
1
vote
0 answers

Interop, overlapped I/O, handles: use SafeHandle or pin?

When you are passing an unmanaged handle (stored in either IntPtr or SafeHandle at the managed side) from managed to unmanaged code to do overlapped I/O, what is the correct approach? use a SafeHandle to wrap the (IntPtr) OS handle in, or use…
Luc VdV
  • 1,088
  • 9
  • 14
1
2