@@ -156,9 +156,6 @@ CFunction::CFunction(unsigned long ulAddr, object oCallingConvention, object oAr
156156 m_eCallingConvention = CONV_CUSTOM;
157157 m_oCallingConvention = oCallingConvention (m_tArgs, m_eReturnType);
158158 m_pCallingConvention = extract<ICallingConvention*>(m_oCallingConvention);
159-
160- // Reserve a Python reference for DynamicHooks.
161- Py_INCREF (m_oCallingConvention.ptr ());
162159 }
163160
164161 // Step 4: Get the DynCall calling convention
@@ -198,7 +195,6 @@ CFunction::CFunction(const CFunction& obj)
198195 {
199196 m_pCallingConvention = obj.m_pCallingConvention ;
200197 m_oCallingConvention = obj.m_oCallingConvention ;
201- Py_INCREF (m_oCallingConvention.ptr ());
202198 }
203199}
204200
@@ -207,20 +203,16 @@ CFunction::~CFunction()
207203 if (!m_pCallingConvention)
208204 return ;
209205
210- // If the convention isn't flagged as hooked, then we need to take care of it.
211- if (!m_pCallingConvention->m_bHooked )
206+ if (m_eCallingConvention != CONV_CUSTOM)
212207 {
213- // If we don't have a Python instance, then we can safely delete it.
214- if (m_oCallingConvention.is_none ())
208+ // If we are using a built-in convention that is currently hooked, let's flag it as no longer hooked
209+ // so that we know we are not bound to a CFunction anymore and can be deleted.
210+ if (m_pCallingConvention->m_bHooked )
211+ m_pCallingConvention->m_bHooked = false ;
212+ // If the convention isn't flagged as hooked, then we need to take care of it.
213+ else
215214 delete m_pCallingConvention;
216- // Otherwise, just release our reference and let Python take care of it.
217- else if (Py_REFCNT (m_oCallingConvention.ptr ()) > 1 )
218- Py_DECREF (m_oCallingConvention.ptr ());
219215 }
220- // If we are using a built-in convention that is currently hooked, let's flag it as no longer hooked
221- // so that we know we are not bound to a CFunction anymore and can be deleted.
222- else if (m_eCallingConvention != CONV_CUSTOM && !dynamic_cast <ICallingConventionWrapper *>(m_pCallingConvention))
223- m_pCallingConvention->m_bHooked = false ;
224216
225217 m_pCallingConvention = NULL ;
226218}
@@ -411,6 +403,10 @@ void CFunction::AddHook(HookType_t eType, PyObject* pCallable)
411403
412404 if (!pHook) {
413405 pHook = HookFunctionHelper ((void *) m_ulAddr, m_pCallingConvention);
406+
407+ // Reserve a Python reference for DynamicHooks.
408+ if (m_eCallingConvention == CONV_CUSTOM)
409+ Py_INCREF (m_oCallingConvention.ptr ());
414410 }
415411
416412 // Add the hook handler. If it's already added, it won't be added twice
@@ -430,6 +426,10 @@ bool CFunction::AddHook(HookType_t eType, HookHandlerFn* pFunc)
430426
431427 if (!pHook)
432428 return false ;
429+
430+ // Reserve a Python reference for DynamicHooks.
431+ if (m_eCallingConvention == CONV_CUSTOM)
432+ Py_INCREF (m_oCallingConvention.ptr ());
433433 }
434434
435435 pHook->AddCallback (eType, pFunc);
@@ -468,10 +468,17 @@ void CFunction::DeleteHook()
468468 Py_DECREF (pOwner);
469469 }
470470 }
471- // If we are a built-in convention bound to a CHook instance but not flagged as hooked anymore, then that
472- // means we are no longer bound to a CFunction instance and can be safely deleted.
473- else if (!pHook->m_pCallingConvention ->m_bHooked )
474- delete pHook->m_pCallingConvention ;
471+ else
472+ {
473+ // If we are using a built-in convention that is currently hooked, let's flag it as no longer hooked
474+ // so that we know we are not bound to a CHook anymore and can be deleted.
475+ if (pHook->m_pCallingConvention ->m_bHooked )
476+ pHook->m_pCallingConvention ->m_bHooked = false ;
477+ // If we are a built-in convention bound to a CHook instance but not flagged as hooked anymore, then that
478+ // means we are no longer bound to a CFunction instance and can be safely deleted.
479+ else
480+ delete pHook->m_pCallingConvention ;
481+ }
475482
476483 // Set the calling convention to NULL, because DynamicHooks will delete it otherwise.
477484 pHook->m_pCallingConvention = NULL ;
0 commit comments