Summary
This chapter provided an in-depth look at various techniques and concepts for low-level programming, primarily involving authoring unsafe code and code that interacts with unmanaged code. It covered essential topics such as memory allocation, pointers, fixed statements, and interop mechanisms, including P/Invoke, COM Interop, Interop Marshaling, and the SafeHandle
class.
We started by reviewing what managed and unmanaged memory are and how we can allocate memory that the GC does not manage. This becomes essential when we have operations that need to be as performant as possible, which means we want to reduce the performance overhead of having the GC involved in allocating and deallocating the memory. This consequently also means that we must be more vigilant when writing this code as we introduce the risk of memory leaks, among other risks. Mechanisms such as stackalloc
and the Marshal
class give us functions that support the allocation and clean-up operations. Still, the...