public static unsafe void SafeCoTaskMemFree(void *pv) { // Even though CoTaskMemFree is a no-op for NULLs, skipping the interop call entirely is faster if (pv != null) { PInvokeMarshal.CoTaskMemFree(new IntPtr(pv)); } }