PyObject_GC_Del() private method

private PyObject_GC_Del ( IntPtr tp ) : void
tp IntPtr
return void
Esempio n. 1
0
 /// <summary>
 /// Common finalization code to support custom tp_deallocs.
 /// </summary>
 public static void FinalizeObject(ManagedType self)
 {
     ClearObjectDict(self.pyHandle);
     Runtime.PyObject_GC_Del(self.pyHandle);
     // Not necessary for decref of `tpHandle`.
     self.FreeGCHandle();
 }
Esempio n. 2
0
        public static void Finalize(IPythonDerivedType obj)
        {
            FieldInfo fi   = obj.GetType().GetField("__pyobj__");
            CLRObject self = (CLRObject)fi.GetValue(obj);

            // delete the python object in an asnyc task as we may not be able to acquire
            // the GIL immediately and we don't want to block the GC thread.
            var t = Task.Factory.StartNew(() =>
            {
                // If python's been terminated then there's nothing to do
                if (0 == Runtime.Py_IsInitialized())
                {
                    return;
                }

                IntPtr gs = Runtime.PyGILState_Ensure();
                try
                {
                    // the C# object is being destroyed which must mean there are no more
                    // references to the Python object as well so now we can dealloc the
                    // python object.
                    IntPtr dict = Marshal.ReadIntPtr(self.pyHandle, ObjectOffset.DictOffset(self.pyHandle));
                    if (dict != IntPtr.Zero)
                    {
                        Runtime.Decref(dict);
                    }
                    Runtime.PyObject_GC_Del(self.pyHandle);
                    self.gcHandle.Free();
                }
                finally
                {
                    Runtime.PyGILState_Release(gs);
                }
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Standard dealloc implementation for instances of reflected types.
        /// </summary>
        public static void tp_dealloc(IntPtr ob)
        {
            ManagedType self = GetManagedObject(ob);

            tp_clear(ob);
            Runtime.PyObject_GC_UnTrack(self.pyHandle);
            Runtime.PyObject_GC_Del(self.pyHandle);
            self.FreeGCHandle();
        }
Esempio n. 4
0
        public static void tp_dealloc(IntPtr ob)
        {
            ManagedType self = GetManagedObject(ob);
            IntPtr      dict = Marshal.ReadIntPtr(ob, ObjectOffset.ob_dict);

            Runtime.Decref(dict);
            Runtime.PyObject_GC_UnTrack(self.pyHandle);
            Runtime.PyObject_GC_Del(self.pyHandle);
            Runtime.Decref(self.tpHandle);
            self.gcHandle.Free();
        }
Esempio n. 5
0
        protected virtual void Dealloc()
        {
            var type = Runtime.PyObject_TYPE(this.ObjectReference);

            Runtime.PyObject_GC_Del(this.pyHandle);
            // Not necessary for decref of `tpHandle` - it is borrowed

            this.FreeGCHandle();

            // we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
            Runtime.XDecref(type.DangerousGetAddress());
        }
Esempio n. 6
0
        public static void Finalize(IPythonDerivedType obj)
        {
            FieldInfo fi   = obj.GetType().GetField("__pyobj__");
            var       self = (CLRObject)fi.GetValue(obj);

            // If python's been terminated then just free the gchandle.
            lock (Runtime.IsFinalizingLock)
            {
                if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
                {
                    if (self.gcHandle.IsAllocated)
                    {
                        self.gcHandle.Free();
                    }
                    return;
                }
            }

            // delete the python object in an async task as we may not be able to acquire
            // the GIL immediately and we don't want to block the GC thread.
            // FIXME: t isn't used
            Task t = Task.Factory.StartNew(() =>
            {
                lock (Runtime.IsFinalizingLock)
                {
                    // If python's been terminated then just free the gchandle.
                    if (0 == Runtime.Py_IsInitialized() || Runtime.IsFinalizing)
                    {
                        if (self.gcHandle.IsAllocated)
                        {
                            self.gcHandle.Free();
                        }
                        return;
                    }

                    IntPtr gs = Runtime.PyGILState_Ensure();
                    try
                    {
                        // the C# object is being destroyed which must mean there are no more
                        // references to the Python object as well so now we can dealloc the
                        // python object.
                        Runtime.PyObject_GC_Del(self.pyHandle);
                        self.gcHandle.Free();
                    }
                    finally
                    {
                        Runtime.PyGILState_Release(gs);
                    }
                }
            });
        }
Esempio n. 7
0
        //====================================================================
        // Common finalization code to support custom tp_deallocs.
        //====================================================================

        public static void FinalizeObject(ManagedType self)
        {
            Runtime.PyObject_GC_Del(self.pyHandle);
            Runtime.Decref(self.tpHandle);
            self.gcHandle.Free();
        }
Esempio n. 8
0
 public static void tp_free(IntPtr tp)
 {
     Runtime.PyObject_GC_Del(tp);
 }