public LiveModel(Configuration config, FactoryContext factoryContext) : base(BindConstructorArguments(config, factoryContext), new Delete <LiveModel>(NativeMethods.DeleteLiveModel)) { this.managedErrorCallback = new NativeMethods.managed_background_error_callback_t(this.WrapStatusAndRaiseBackgroundError); // DangerousGetHandle here is trivially safe, because .Dispose() cannot be called before the object is // constructed. NativeMethods.LiveModelSetCallback(this.DangerousGetHandle(), this.managedErrorCallback); this.managedTraceCallback = new NativeMethods.managed_trace_callback_t(this.SendTrace); }
private static New <LiveModel> BindConstructorArguments(Configuration config, FactoryContext factoryContext) { return(new New <LiveModel>(() => { factoryContext = factoryContext ?? new FactoryContext(); IntPtr result = NativeMethods.CreateLiveModel(config.DangerousGetHandle(), factoryContext.DangerousGetHandle()); // These references do not live on the heap in this delegate, and could disappear during the invocation // of CreateLiveModel. Thus, we need to ensure GC knows not to release them until after that call // returns. GC.KeepAlive(config); GC.KeepAlive(factoryContext); return result; })); }