New Menus Memory Leak Issue

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

New Menus Memory Leak Issue

Post by Archive-Poster »

Code: Select all

#include <amxmodx>

public plugin_init()
{
    register_clcmd("say /test", "OnSayTest");
}

public OnSayTest(client)
{
    new menu = menu_create("Test Menu", "OnMenu");
    
    menu_additem(menu, "Item");
    menu_display(client, menu);
    
    return PLUGIN_HANDLED;
}

public OnMenu(client, menu, item)
{
    menu_destroy(menu);
    
    return PLUGIN_HANDLED;
} 
say /test → radio1/radio2/radio3/open any other menu → memory leak - Test Menu is never destroyed.

Any workarounds? Caching menus isn't really an option when using ML or when the menu consists of players. Is it worth filing as a bug report?
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

Re: New Menus Memory Leak Issue

Post by Archive-Poster »

This should do.

Code: Select all

/*
 * Shows a menu which is supposed to be destroyed when another one is shown.
 * It does a certain check and then calls menu_display().
 * Use instead of menu_display().
 * 
 * @return - same as menu_display().
 */
stock ShowLocalMenu(client, menu, page = 0);

/*
 * Shows a menu which is NOT supposed to be destroyed when another one is shown.
 * It does a certain check and then calls menu_display().
 * Use instead of menu_display().
 * 
 * @return - same as menu_display().
 */
stock ShowGlobalMenu(client, menu, page = 0);

/*
 * Destroys a menu which was previously shown with ShowLocalMenu() function.
 * Use instead of menu_destroy().
 * 
 * @return - same as menu_destroy().
 * 
 * Note - It requires the index of player as first parameter, unlike menu_destroy().
 * Note - For global menus, which were shown with ShowGlobalMenu() or menu_display(),
 *        use menu_destroy() instead.
 */
stock DestroyLocalMenu(client, menu);
Attachments
safemenu.inc.zip
(684 Bytes) Downloaded 6 times
User avatar
Archive-Poster
Posts: 1347
Gender:
United States of America

Re: New Menus Memory Leak Issue

Post by Archive-Poster »

Code: Select all

#include <amxmodx>
#include <safemenu>

public plugin_init()
{
    register_clcmd("say /test", "OnSayTest");
}

public OnSayTest(client)
{
    new menu = menu_create("Test Menu", "OnMenu");
    
    menu_additem(menu, "Item");
    ShowLocalMenu(client, menu);
    
    return PLUGIN_HANDLED;
}

public OnMenu(client, menu, item)
{
    client_print(0, print_chat, "test");
    
    DestroyLocalMenu(client, menu);
    
    return PLUGIN_HANDLED;
} 
Only when I press 1 or 0 it works.
It doesn't call OnMenu(), it destroys menus which were displayed using Show[Local|Global]Menu() when another Show[Local|Global]Menu() is used. So as long as all the plugins use Show[Local|Global]Menu() instead of menu_display(), only a maximum of 1 temporary ("local") menu is held in the memory for players separately. Without this an unlimited amount of memory leak would be possible.

I could've made it call the menu (OnMenu()) with item as MENU_EXIT, but it wouldn't be any different, really.
You can check it by creating menu A and showing it, then creating menu B and showing it without first destroying menu A. You'll see how menu A has an ID of 0 while menu B has an ID of 1. However menu A is no longer available, so the next menu (C) will have an ID of 0.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest