mirror of
				https://github.com/VCMP-SqMod/SqMod.git
				synced 2025-11-04 08:17:19 +01:00 
			
		
		
		
	Implement a new module command that is called right before cosing the virtual machine to allow modules to release resources manually.
Should fix a possible crash in the IRC plugin which was caused by the Session destructor to be called recursively when cleaned automatically by the VM.
This commit is contained in:
		@@ -99,6 +99,15 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    NullFunction().ReleaseGently();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
    // Terminate all sessions, if any
 | 
			
		||||
    SessionTerminate();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -106,8 +115,6 @@ static void OnSquirrelReleased()
 | 
			
		||||
{
 | 
			
		||||
    // Release the current virtual machine, if any
 | 
			
		||||
    DefaultVM::Set(nullptr);
 | 
			
		||||
    // Terminate all sessions, if any
 | 
			
		||||
    SessionTerminate();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
@@ -141,6 +148,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -202,7 +202,9 @@ void Session::Destroy()
 | 
			
		||||
{
 | 
			
		||||
    // Make sure there's even a session to release
 | 
			
		||||
    if (!m_Session)
 | 
			
		||||
	{
 | 
			
		||||
        return;
 | 
			
		||||
	}
 | 
			
		||||
    // Disconnect the session
 | 
			
		||||
    Disconnect();
 | 
			
		||||
    // Break the association with this instance (paranoia)
 | 
			
		||||
 
 | 
			
		||||
@@ -102,6 +102,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    NullFunction().ReleaseGently();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -142,6 +150,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -186,6 +186,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -226,6 +234,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -78,6 +78,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -118,6 +126,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -95,6 +95,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -135,6 +143,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -89,6 +89,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -129,6 +137,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -99,6 +99,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -139,6 +147,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -377,6 +377,14 @@ static void OnSquirrelTerminate()
 | 
			
		||||
    // Release script resources...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined is about to be closed. Last chance to release anything manually.
 | 
			
		||||
*/
 | 
			
		||||
static void OnSquirrelClosing()
 | 
			
		||||
{
 | 
			
		||||
	// Nothing to release manually...
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ------------------------------------------------------------------------------------------------
 | 
			
		||||
 * The virtual machined was closed and all memory associated with it was released.
 | 
			
		||||
*/
 | 
			
		||||
@@ -417,6 +425,10 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelTerminate();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_CLOSING_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelClosing();
 | 
			
		||||
        } break;
 | 
			
		||||
        case SQMOD_RELEASED_CMD:
 | 
			
		||||
        {
 | 
			
		||||
            OnSquirrelReleased();
 | 
			
		||||
 
 | 
			
		||||
@@ -58,6 +58,7 @@ extern "C" {
 | 
			
		||||
    #define SQMOD_INITIALIZE_CMD    0xDABBAD00
 | 
			
		||||
    #define SQMOD_LOAD_CMD          0xDEADBABE
 | 
			
		||||
    #define SQMOD_TERMINATE_CMD     0xDEADC0DE
 | 
			
		||||
    #define SQMOD_CLOSING_CMD       0xBAAAAAAD
 | 
			
		||||
    #define SQMOD_RELEASED_CMD      0xDEADBEAF
 | 
			
		||||
    #define SQMOD_API_VER           1
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -513,6 +513,10 @@ void Core::Terminate(bool shutdown)
 | 
			
		||||
        // Assertions during close may cause double delete/close!
 | 
			
		||||
        HSQUIRRELVM sq_vm = m_VM;
 | 
			
		||||
        m_VM = nullptr;
 | 
			
		||||
 | 
			
		||||
        cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins the virtual machine is closing");
 | 
			
		||||
        // Tell modules to do their monkey business
 | 
			
		||||
        _Func->SendPluginCommand(SQMOD_CLOSING_CMD, "");
 | 
			
		||||
        // Attempt to close the VM
 | 
			
		||||
        sq_close(sq_vm);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user