Search code examples
androidc++opengl-esandroid-ndknative-activity

Android NDK NativeActivity Memory Management Needed?


I'm using NativeActivty with native_app_glue to program an OpenGL game for Android. It is hard to determine when is the best time to delete heap allocated objects and objects allocated by OpenGL.

In the NDK's NativeActivity sample they don't seem to delete objects after they are done with them. Right now I delete my objects with two delete functions: one for OpenGL objects and a regular one. I split them up because it seems only the event handling thread can delete OpenGL objects. Regular objects I allocate are deleted by the normal delete function.

When the user presses the back button and comes back to the app, the OpenGL resources are deleted and the app crashes (although I would think they should be reallocated by APP_CMD_INIT_WINDOW). This causes me to wonder if I should be deleting objects at all because it appears I should not delete OpenGL objects.

Are we supposed to delete heap allocated objects with the NDK. I understand that NDK apps are still sandboxed and have a JVM process, does this take car of memory management?

EDIT: Another instance of my application crashing is when objects are deleted when a game state finishes. For example going from the actual game to the menu. Upon going back to the game the application crahses, even though the objects are reallocated. It only seems to work once.


Solution

  • It turns out the application crashing was never due to the deletion of heap allocated objects or OpenGL objects. Some pointers that were deleted but still contained in a vector were the cause of the problem. However it appears there is nothing wrong with deleting objects with the NDK.