Memory leak with hud code?

Sorry to ask this but when i add this code to my cl init for my avatar to be shown on the hud it creates memory leak this is were it starts to get laggier the more you on the server any help?

here the code if you think you can.


local av = vgui.Create("AvatarImage", frame)

	av:SetPos(17,1015)
	av:SetSize(32, 32)
	av:SetPlayer( LocalPlayer(), 32 )

Take the code out of your HUDPaint hook and put it in the Initialize hook maybe?

You are creating a new avatar image every time the hud draws, over top of the old one. In a short period of time you will have thousands of avatar images in the same spot.

Gonna try this and see if it works :slight_smile:


function cool()
local av = vgui.Create("AvatarImage", frame)
	av:SetPos(17,500)
	av:SetSize(32, 32)
	av:SetPlayer( LocalPlayer(), 32 )
end
hook.Add( "Initialize", "cool", init )

[editline]17th December 2013[/editline]

this dosen’t work :frowning:

third argument of hook.Add is the hook, so, should say cool.

second argument of vgui.Create might be undefined, unless you define frame as global, if you define it as local inside HUDPaint then it will be nil

edit:
So you know the arguments for hook.Add:
hook.Add( String hook, String unique_name, Function hook_func )
look up the functions, from where did you get your arguments like that?

Post full code if you get nil error

ok will do :slight_smile:

You can use the avata image in a HUDPaint hook if done properly:



local gAvatar
hook.Add("HUDPaint", "myhudwhatever", function()
	if ( !IsValid( gAvatar ) ) then
		gAvatar = vgui.Create("AvatarImage")
		gAvatar:SetPos(17,1015) -- You'd better use a fraction of ScrW() and ScrH() for muli-resolution result.
		gAvatar:SetSize(32, 32)
		gAvatar:SetPlayer( LocalPlayer(), 32 )
	end
end )


gross gross gross: stop using HUDPaint, make your entire HUD using vgui panels in the init hook and then just:

PANEL:ParentToHUD()