From 62611ac86b1033cc110db2e0cc65357132799a54 Mon Sep 17 00:00:00 2001 From: Dane Finlay Date: Thu, 26 Sep 2024 10:13:53 +1000 Subject: [PATCH] Make natDisconnect() be called upon ext module deallocation (#211) These changes make it so the natDisconnect() function is called for you automatically if you forget it. No effect in-process. --- NatlinkSource/DragonCode.cpp | 7 +++++++ NatlinkSource/DragonCode.h | 4 ++++ NatlinkSource/pythwrap.cpp | 22 +++++++++++++++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/NatlinkSource/DragonCode.cpp b/NatlinkSource/DragonCode.cpp index b7d57952..0ac91396 100644 --- a/NatlinkSource/DragonCode.cpp +++ b/NatlinkSource/DragonCode.cpp @@ -694,6 +694,13 @@ BOOL CDragonCode::isNatSpeakRunning() //--------------------------------------------------------------------------- +void CDragonCode::freeModule() +{ + natDisconnect(); +} + +//--------------------------------------------------------------------------- + void CDragonCode::releaseObjects() { // iterate over all the grammar objects and free them; note that when we diff --git a/NatlinkSource/DragonCode.h b/NatlinkSource/DragonCode.h index 093202fb..38102b73 100644 --- a/NatlinkSource/DragonCode.h +++ b/NatlinkSource/DragonCode.h @@ -114,6 +114,10 @@ class CDragonCode void setAppClass( CDgnAppSupport * pAppClass ) { m_pAppClass = pAppClass; } void setDuringInit( BOOL bState ) { m_bDuringInit = bState; } + // this is called when the natlink module (this class!) is deallocated from + // Python + void freeModule(); + // these functions are called from CGrammarObject ISRCentral * pISRCentral() { return m_pISRCentral; } void addGramObj(CGrammarObject * pGramObj ); diff --git a/NatlinkSource/pythwrap.cpp b/NatlinkSource/pythwrap.cpp index 12e24b0f..73eeedc5 100644 --- a/NatlinkSource/pythwrap.cpp +++ b/NatlinkSource/pythwrap.cpp @@ -2317,16 +2317,28 @@ static struct PyMethodDef natlink_methods[] = { { NULL } }; +//--------------------------------------------------------------------------- +// natlink module free function. + +void natlink_free( void * p ) +{ + cDragon.freeModule(); +} + //--------------------------------------------------------------------------- // import natlink from Python // // We tell Python about our functions and also create an error type. static struct PyModuleDef NatlinkModule = { - PyModuleDef_HEAD_INIT, - "_natlink_core", /* name of module */ - "natlink with python3 compatability", - -1, - natlink_methods + PyModuleDef_HEAD_INIT, /* m_base */ + "_natlink_core", /* m_name */ + "natlink with python3 compatability", /* m_doc */ + -1, /* m_size */ + natlink_methods, /* m_methods */ + NULL, /* m_slots */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + natlink_free /* m_free */ };