-
-
Notifications
You must be signed in to change notification settings - Fork 464
Fix xmlLoadString memory leak #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, mate.
Possible to incorporate smart pointers? |
Well, kinda? But I woudn't, because it woudnt be proper use of them IMHO. Lua itself won't hold the reference count up, so its kinda mix and match. |
I'd have to actually learn about smart pointers - eek! If someone else wants to add that to this PR very, very soon then go ahead - otherwise I think we should just merge as is (1.5.8 overdue!) |
Does this really fix the memory leak? The memory gets freed when the destructor of CLuaMain is called, and CLuaMain is only called when the client exits the server. So if the client never leaves the server, then the memory will keep on stacking up. |
I monitored the memory usage on both the client and server using the following script (edit: just added an updated version which appears to work the same) local xml = {}
local instances = 10000
function xmlStringTest()
for i=1, instances do
xml[#xml+1] = xmlLoadString("<animals><animal type='monkey'></animal></animals>")
end
end
addCommandHandler(triggerClientEvent and "sxml" or "cxml", xmlStringTest) When restarting/stopping the resource, the memory is freed on both client and server (and freed for the client when leaving the server). I guess one thing I didn't think of is how you can free the memory from Lua without needing to restart/stop the resource. In that case do we need to add a function such as |
I see. CLuaMain is per resource.
I think extending xmlUnloadFile to support unloading of loaded strings would be nice. |
Would it not make more sense (naming wise) to create |
Technically, a loaded string is also a file. To be honest, I would've just added an extra optional paramter to xmlLoadFile for loading xml files as strings, but now since we have a function, I don't think it matters that much if we use xmlUnloadFile for unloading xml files that were loaded as strings. |
For what it's worth @saml1er I'd argue we can still remove |
That makes sense. We haven't officially released it. We can remove xmlLoadString and extend xmlLoadFile. |
Possible to keep it consistent with other functions (e.g. engineLoadTXD/playSound/dxCreateShader) and use a heuristic? The first two examples above are binary but this is the heuristic used for dxCreateShader: mtasa-blue/Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.cpp Lines 1171 to 1184 in 36efacb
Perhaps if it satisfies any of the following properties:
Maybe a heuristic is a bad idea in this case, since on Linux, |
If it's longer than 256 characters it must raw data*. If that fails, check for other things, that'd be the fastest IMHO *Well, kinda. Windows now supports more than 256 afaik, but, nobody uses more than that. |
We would need to use existing MTA file functions to verify the path/file exists and is a valid resource file. Character length and/or checking included characters isn't good enough in this situation. |
The suggested changes have deviated too much from what the initial PR intended to do - I'll close this and submit a new PR soon which re-implements XML string loading into |
I mean, we can merge this (once it uses smart pointers), and you can do the xmlLoadFile/xmlLoadString merge in a separate PR. That way there's one PR for the bugfix and a separate PR for the refactor. (If you prefer to do it in one PR, please use separate commits for the bugfix and the refactor.) |
I'm happy to just let this one through in favour of getting this fixed ASAP. This is due a refactor anyway |
Resolves #1373