Memory leak in Mesh(). Making Sprites

I was thinking about how best to create sprites. I tried two methods (a) creating a local mesh on the fly and assigning it different texture coordinates as the sprite animates. clear the mesh after drawn. (b) pre-load all the frames in an array of mesh. I thought (a) would use up less memory as the mesh is generated on the fly, while (b) stores a huge amount of data (if you have lots of sprites) in the memory but might be faster run-time.

(a) actually crashes codea after 1000+ cycles, even with collectgarbage() and using only local meshes. It works fine on the ios simulator in xcode and in iphone/ipad. This leads me to think that there’s something about the mesh() in codea that has a memory leak and is causing codea to crash.

As expected, (b) has a small initial load time but runs slightly faster in run time.

Could you try (a) but just change the texture coords (don’t clear the mesh) and also try without localising the mesh?

@zapaper try using xCode to observe memory usage, I’m pretty sure the runtime for Codea and the runtime for xCode are designed to be exactly the same, literally identical so it may not crash on your iOS simulator because of the higher amount of memory on the mac compared to the iPad.

Also as Coder said make one mesh one time at the start or when needed and then just modify the mesh, takes up far less memory than it does to create a mesh every time.

Will try that!

Using a mesh single mesh and using Mesh:setRect and Mesh:setRecTex to change the position and texture works great. as long as you dont want to rotate the rectangels.

@Thwapp you can rotate with

myMesh:setRect(index,x,y,width,height,rotation)

By adding a 6th parameter

I should FTFM. :slight_smile: Thx

Ok Luatee is right: you have to create meshes on the start and only modify coords etc later. But if you need to recover memory, is there a possibility to remove meshes from memory?

Good question @Gionbe, is there a way to clear meshes from memory when no longer need?

@Rodolphe

mesh:clear()
mesh = nil

That’s all I know

oh alright, thanks a lot!

@zapaper is it possible to get the code which exposes the leak? Would love to profile this and fix.

wow, this is some time ago; not sure if I still hv the code that tested the two options. Essentially, say I have a spritesheet for NPC/monsters. I could pre-load all the meshes (method b), or change the texcoord/file each time I draw (method a). Method A will crash codea after awhile, but seemed to run alright on simulator.

Sorry @zapaper, the thread got bumped and I didn’t pay attention to your original posting date! I wonder if this has been fixed since.