Raised This Month: $12 Target: $400
 3% 

New Menus Memory Leak Issue


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 01-30-2013 , 16:34   New Menus Memory Leak Issue
Reply With Quote #1

PHP Code:
#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(clientmenu);
    
    return 
PLUGIN_HANDLED;
}

public 
OnMenu(clientmenuitem)
{
    
menu_destroy(menu);
    
    return 
PLUGIN_HANDLED;

say /testradio1/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?
__________________
hleV is offline
Sp@jk
Member
Join Date: May 2010
Location: Serbia
Old 01-30-2013 , 18:42   Re: New Menus Memory Leak Issue
Reply With Quote #2

you could set a timer to destroy the menu and reshow it
Sp@jk is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-30-2013 , 20:19   Re: New Menus Memory Leak Issue
Reply With Quote #3

Since the menu is client specific, cache menu id per player and always destroy before starting new menu (not sure if destroying an invalid menu will cause an error or now though). Also, loop through all players' menu id's on plugin_end() (if you "think" the menus are not destroyed on mapchange).

This suggestion is similar to handling player specific tasks.

OR, if there is a way to check if a menu is valid (based on the last cached menu id) you might be able to just display it (but it won't be updated with the latest values).

P.S. Your thread is not a tutorial or a code snippet and therefor does not belong in this forum.
__________________

Last edited by fysiks; 01-30-2013 at 20:21.
fysiks is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 01-30-2013 , 20:56   Re: New Menus Memory Leak Issue
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
Your thread is not a tutorial or a code snippet and therefor does not belong in this forum.
I kinda wanted to post this in the new menus tutorial thread, but figured that people would probably not notice that and continue creating menus that leak memory. This could be treated as a tutorial about how not to leak memory with new menus, as long as the best way to do that is provided. So far it only addresses the issue, with some ideas how to prevent it, but I think it's fine in this section.
__________________
hleV is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-30-2013 , 21:12   Re: New Menus Memory Leak Issue
Reply With Quote #5

Quote:
Originally Posted by hleV View Post
I kinda wanted to post this in the new menus tutorial thread, but figured that people would probably not notice that and continue creating menus that leak memory. This could be treated as a tutorial about how not to leak memory with new menus, as long as the best way to do that is provided. So far it only addresses the issue, with some ideas how to prevent it, but I think it's fine in this section.
Honestly, I'm not convinced there is a leak. How did you "prove" there is a leak?
__________________
fysiks is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 01-30-2013 , 21:19   Re: New Menus Memory Leak Issue
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
Honestly, I'm not convinced there is a leak. How did you "prove" there is a leak?
When a menu is shown "over" of a new menu, the new menu handler is suppose to be called with MENU_EXIT. A simple print test with his code reveals that the handler function is not called when a radio menu is brought up, so there definitely is a leak.

I would consider this a bug though, as it should be calling the handler function with MENU_EXIT.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-30-2013 , 21:25   Re: New Menus Memory Leak Issue
Reply With Quote #7

Quote:
Originally Posted by Emp` View Post
When a menu is shown "over" of a new menu, the new menu handler is suppose to be called with MENU_EXIT. A simple print test with his code reveals that the handler function is not called when a radio menu is brought up, so there definitely is a leak.

I would consider this a bug though, as it should be calling the handler function with MENU_EXIT.
Yeah, I just did some of my own testing.

So, then the question is how often does this happen? If it's rare enough it can be ignored since memory is destroyed on mapchange (right?).
__________________
fysiks is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 02-04-2013 , 11:47   Re: New Menus Memory Leak Issue
Reply With Quote #8

This should do.
PHP Code:
/*
 * 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(clientmenupage 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(clientmenupage 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(clientmenu); 
Attached Files
File Type: inc safemenu.inc (1.5 KB, 436 views)
__________________

Last edited by hleV; 02-04-2013 at 15:14.
hleV is offline
Kiske
Veteran Member
Join Date: May 2009
Old 02-04-2013 , 17:34   Re: New Menus Memory Leak Issue
Reply With Quote #9

PHP Code:
#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(clientmenu);
    
    return 
PLUGIN_HANDLED;
}

public 
OnMenu(clientmenuitem)
{
    
client_print(0print_chat"test");
    
    
DestroyLocalMenu(clientmenu);
    
    return 
PLUGIN_HANDLED;

Only when I press 1 or 0 it works.
__________________


Last edited by Kiske; 02-04-2013 at 17:35.
Kiske is offline
Send a message via Skype™ to Kiske
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 02-05-2013 , 09:11   Re: New Menus Memory Leak Issue
Reply With Quote #10

Quote:
Originally Posted by Kiske View Post
PHP Code:
#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(clientmenu);
    
    return 
PLUGIN_HANDLED;
}

public 
OnMenu(clientmenuitem)
{
    
client_print(0print_chat"test");
    
    
DestroyLocalMenu(clientmenu);
    
    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.
__________________

Last edited by hleV; 02-05-2013 at 09:17.
hleV is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode