private void RegisterImpl(HandleRef unmanaged) { if (!Handles.ContainsKey(unmanaged.Handle)) { Handles.Add(unmanaged.Handle, new Reference(GCHandle.Alloc(unmanaged.Wrapper, GCHandleType.Weak))); } }
private void RegisterImpl(IntPtr unmanaged) { if (!Handles.ContainsKey(unmanaged)) { Handles.Add(unmanaged, new Reference(GCHandle.Alloc(null, GCHandleType.Weak))); } }
public ReferencedObject(SqHostObject hostObj, object obj, out ulong handleIndex) { try { Handle = GCHandle.Alloc(obj, GCHandleType.Normal); } catch (Exception e) { throw new Exception($"Could not allocate object of type {obj.GetType()} when making an SqHostObject: {e.ToString()}"); } ReleaseHook = new Squirrel.Unmanaged.SqReleaseHook( (ptr, _) => { unsafe { Handles[*(ulong *)ptr].Handle.Free(); Handles.Remove(*(ulong *)ptr); } return(1); } ); Index = NextHandleIndex; var attemptsToFindNextIndex = 0UL; do { NextHandleIndex = unchecked (NextHandleIndex + 1); attemptsToFindNextIndex++; } while (attemptsToFindNextIndex < ulong.MaxValue && Handles.ContainsKey(NextHandleIndex)); handleIndex = Index; Handles.Add(handleIndex, this); hostObj.PushSelf(); ScriptHandler.Squirrel.SetReleaseHook(-1, ReleaseHook); ScriptHandler.Squirrel.Pop(1); }