I have the following code:
using namespace System::Runtime::InteropServices;
void CRCWAssembly::CallCOMMethod(long __gc* status)
IntPtr ip = Marshal::AllocHGlobal(sizeof(*status));
if (Marshal::ReadInt32(ipStatus) != 0)
throw new Exception();
throw new Exception("COMMethod", sehEx);
Of course, if an exception is thrown, the memory allocated to ip (IntPtr) never gets freed up. Short of wrapping it up in a class or putting Marshal::FreeHGlobal all over the place and moving the declaration of ip to outside of the 'try' block, is there any other better method to manage this unmanaged memory in the case of an exception being thrown?