is this the proper way to init/deinit Irrlicht engine? I put the code in a loop side by side, and it looks like I am seeing memory leak. The total memory usage keeps growing. By the way I am using Irrlicht 1.9 prerelease, on iOS.
so, what is the correct way to initialize/teardown the engine? or, should I just have the engine initialized once? then when I move from one level to the other, how do I reset the engine context?
Why would you do that? That destroys and recreates the window each time, along with other needless init/deinit things, causing you pointless level load delays (and flickering!).
should I just have the engine initialized once? then when I move from one level to the other, how do I reset the engine context?
Yes, do it once. You can clear out textures and meshes through the scene manager and the respective caches.
Usually you create the device once and release it once per application. Although you shouldn't get a memory-leak in your case. Maybe it needs a call to _pDevice->closeDevice before the drop call.
More often it's sufficient to clear the scenemanager. If you also need to release textures and meshes you can also call IVideoDriver:: removeAllTextures and ISceneManager::getMeshCache()->clear(). For gui it's IGUIEnvironment::clear().
If you want to clear only textures for models, but not for example for the gui then you should remove textures one-by-one and have to remember yourself somewhere which ones to remove and which to keep.
If there's enough memory then the best way is to actually keep meshes and textures in memory all the time. For a game with many huge scenes that is impossible, but for many games it works.
Following the suggestion I keep the device open across different levels. So I try to clean up the resources without shutting down the device. One of the issue I am facing is "addHighLevelShaderMaterialFromFiles". I am using OGLES2 so internally it creates a COGLES2MaterialRenderer. However I don't know what is the correct way to trigger the destruction of that class.
So, is there a way to clean it up? or I should keep all the materials cross levels too? It is kind of messy to keep them in memory since the user data could be changed from level to level but I don't see a way to update the material callback class...
I think there is currently no way to remove materials. Generally you should be fine by adding materials just once (unless you write really a lot of shaders for each level). I don't know now if there was some technical reason why there is no cleanup for shaders in engine, I wondered about that myself in the past.
When function "addHighLevelShaderMaterial" is called, one of the parameter is "userData", which will be referenced inside the callback function. I don't see a problem to have those materials stay in memory but the userData will definitely change from time to time. Ideally there should be some API to allow changing the userData on the fly.
For now I will use an indirect pointer so that I could change the userData on my side.