Showing posts with label .NET CLR. Show all posts
Showing posts with label .NET CLR. Show all posts

Thursday, April 22, 2010

Preventing method inlining in c#

I sometimes want to prevent inlining of methods byt the JIT in C#. Usually this is because I need the call stack to be preserved, for example when using Assembly.GetCallingAssembly.

This is the attribute:
[MethodImpl(MethodImplOptions.NoInlining)]

Sunday, April 18, 2010

Different types of SafeHandle

I ran into the following error:
"Cannot marshal 'return value': Returned SafeHandles cannot be abstract."

The Microsoft.Win32.SafeHandles namespace has several different handle types.

Thursday, February 18, 2010

Using minidumps in .NET 4.0

.NET 4.0 provides for post mortem debugging of minudumps - but only if they include a full memory dump (which makes them not so 'mini' any more). This post list tips for using these dumps.

ProcDump can be used to make dumps in various scenarios, including high CPU usage, unhandled exception, etc.

Monday, February 1, 2010

Preserving the complete stack trace when rethrowing an exception

Even throw; loses the top level of the stack trace. Use this 'hack' to work around the problem:

static void PreserveStackTrace(Exception exception)
{
    MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
        BindingFlags.Instance | BindingFlags.NonPublic);
    preserveStackTrace.Invoke(exception, null);
}

(from here)

Tuesday, September 22, 2009

WinDbg with managed code

Loading the correct version of SOS is done by: .loadby sos mscorwks.

Useful commands in SOS:
List CLR threads: !threads
Dump managed stack of current thread: !CLRStack
Dump managed stack of current thread with parameters and locals: !CLRStack -a
Dump managed and native stack of current thread: !DumpStack
Break on first chance CLR exceptions: sxe clr
Get IL of method: !ip2md address, then !u result
Display last exception info: !pe (use Event Filters to run the command whenever a CLR exception is thrown)

Another useful tool is SOSEX, which supports deadlock detection, etc.

I was unable to get WinDbg to be the postmortem debugger for all crashes - installing it only seems to work for native applications.

Native commands:
List O/S threads: ~
Change thread by: ~{threadnumber}s or ~~[{threadid}]s
Handle tracing: !htrace

This cheat sheet is also useful.

Wednesday, September 2, 2009

SafeHandle better than IntPtr for O/S HANDLEs

During my recent travels through P/Invoke, I found SafeHandle, which is preferable to IntPtr for handle types.

See: this blog entry.

Unfortunately, as with many good things, it is not available in the Compact Framework. No HandleRef either.

Now I just need to find out whether I should be using IntPtr or UIntPtr...

GCHandle

I seem to struggle to find this little class whenever I need to, and usually only find it after exhausting all the methods on Marshal before eventually finding it using Google.

GCHandle is used to get the address (IntPtr) for an object and can pin the object so it won't be moved while native code is using it.

This blog describes why GCHandle doesn't implement IDisposable.

Trap for young players: if your object is a struct or other value type, it will be boxed and that copy will be pinned, not your original structure. This boxing happens when you pass your instance to the GCHandle constructor and it is cast to the object base class.