alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
990 stars 426 forks source link

CreateMenuEx, ..., DisplayMenu crash @ player disconnection # 1.12.0.7066 #2073

Open ClaudiuHKS opened 1 year ago

ClaudiuHKS commented 1 year ago

1.12.0.7066

While looking at the menu and leaving the game server it crashes without any error logs. It's supposed to be throwing error logs instead. No error logs are thrown while using the plugin at all.

#include <sourcemod>
#include <clientprefs>
#include <sdkhooks>
#include <sdktools>
#include <topmenus>
#include <dhooks>
#include <geoip>
#include <regex>

public void OnPluginStart()
{
    RegConsoleCmd("sm_try", OnPlayerCommandExecution, "sm_try - ...", FCVAR_GAMEDLL | FCVAR_UNLOGGED);
}

public Action OnPlayerCommandExecution(int nPlayer, int nArgs)
{
    static Handle xMenu;
    static Handle xStyle;

    if (nPlayer < 1)
    {
        return Plugin_Stop;
    }

    if (nPlayer > MaxClients)
    {
        return Plugin_Stop;
    }

    if (IsFakeClient(nPlayer))
    {
        return Plugin_Stop;
    }

    xStyle = GetMenuStyleHandle(MenuStyle_Radio);

    if (null == xStyle)
    {
        return Plugin_Stop;
    }

    xMenu = CreateMenuEx(xStyle, OnPlayerMenuExecution, MenuAction_Select | MenuAction_Cancel | MenuAction_End);

    if (null == xMenu)
    {
        CloseHandle(xStyle);

        return Plugin_Stop;
    }

    SetMenuTitle(xMenu, "YOUR ...");
    SetMenuPagination(xMenu, MENU_NO_PAGINATION);
    SetMenuExitButton(xMenu, false);
    SetMenuExitBackButton(xMenu, false);
    SetMenuNoVoteButton(xMenu, false);
    SetMenuOptionFlags(xMenu, MENUFLAG_NO_SOUND);

    AddMenuItem(xMenu, "...", "1...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "2...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "3...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "4...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "5...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "6...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "7...", ITEMDRAW_DEFAULT);
    AddMenuItem(xMenu, "...", "8...", ITEMDRAW_DEFAULT);

    AddMenuItem(xMenu, "", "", ITEMDRAW_SPACER);

    AddMenuItem(xMenu, "", "0...", ITEMDRAW_CONTROL);

    SetMenuTitle(xMenu, "YOUR ...");
    SetMenuPagination(xMenu, MENU_NO_PAGINATION);
    SetMenuExitButton(xMenu, false);
    SetMenuExitBackButton(xMenu, false);
    SetMenuNoVoteButton(xMenu, false);
    SetMenuOptionFlags(xMenu, MENUFLAG_NO_SOUND);

    DisplayMenu(xMenu, nPlayer, MENU_TIME_FOREVER);

    CloseHandle(xStyle);

    return Plugin_Stop;
}

public int OnPlayerMenuExecution(Handle xMenu, MenuAction xAction, int nThePrimaryParameter, int nTheSecondaryParameter)
{
    if (null == xMenu)
    {
        return 1;
    }

    if (MenuAction_Select == xAction)
    {
        // CloseHandle(xMenu); /// this will obviously throw an error log if uncommented

        return 1;
    }

    if (MenuAction_End == xAction)
    {
        CloseHandle(xMenu);

        return 1;
    }

    if (MenuAction_Cancel == xAction)
    {
        CloseHandle(xMenu);

        return 1;
    }

    CloseHandle(xMenu);

    return 1;
}
ClaudiuHKS commented 1 year ago

Update: How am I using the code above without getting any crashes or error logs?

  1. The menu handler -> No CloseHandle() at all.
  2. The CreateMenuEx() call -> After each CreateMenuEx() call, I add its Handle into an Array which elements are released during the OnMapStart() call.