FWGS / xash3d

DEPRECATED in favor of https://github.com/FWGS/xash3d-fwgs. Only bugfixes are accepted.
https://xash.su
GNU General Public License v3.0
549 stars 107 forks source link

Usage of Host_Error() #381

Closed noodlecollie closed 6 years ago

noodlecollie commented 6 years ago

Can anyone explain a bit about the correct usage of Host_Error()? I assumed that it would be the equivalent of shutting down the engine with an error message, but at least in developer mode I just get an error printed to the console and no actual shutdown. Is this intended?

I'm also wondering whether heap memory previously allocated will get freed when Host_Error() is called? I've switched to using Sys_Error() in my current code just in case, but if by some internal magic Host_Error() does keep track of allocated mempools, that'd make my life easier.

a1batross commented 6 years ago

Sys_Error will always lead you to closed engine. Host_Error is way more smarter. It does not close the engine, it's just doing clean-up, jumps to last successful frame(because current is unrecoverable) and moves you to console. It's better to use the Host_ for all non-system code, when error is issued, but it's not so critical and you can recover from erroneus state. When recovering is not possible, engine will be closed.

All heap memory is freed when engine is shutting down. You don't need to worry about memory in Host_Error, it will be freed during the shutting down the game and freeing models.

noodlecollie commented 6 years ago

But is that memory only released when the engine is completely shut down? My worry is that if I allocate a lot of memory for loading a map, for example, and part way through loading I fail with Host_Error, the memory won't be released until the engine is shut down. If I were to attempt to load the same map and fail multiple times, the engine would just continue to eat up this memory without releasing it.

mittorn commented 6 years ago

Mem_FreePool frees entire memory pool. You just need to use pool that will be freed on map unloading

a1batross commented 6 years ago

@x6herbius it depends on which mempool you're using for your data.

For example, you allocate too much memory and associate it with model's mempool. Host_Error calls Mod_ClearAll which loops through all models(brush, studio or sprite models) and calls Mod_FreeModel, which selects unloader. Unloader itself releases model's mempool.

Or you allocate something from server's mempool. It will be freed in SV_Shutdown, which is too called by Host_Error.

Just use appropriate mempool everytime you working with dynamic memory. Engine will care about a rest for you.

You're also can allocate a your own pool and control it by yourself.

noodlecollie commented 6 years ago

That makes sense. I've been using the loadmodel mempool for the map currently being loaded, so I guess it'll all get cleaned up automatically then. Sounds good!

a1batross commented 6 years ago

Yes, for models it always in model_t::mempool.