Texture deletion??

Hi, I am trying to understand how to manage textures.

Unlike many objects, textures do not have a create function. I assumed this meant that I could go ahead and manage the memory of the textures I create myself, but this seems not to be the case.

Therefore, I create a texture object within the context of one of my layers that lives as long as the scene is active, but is automatically freed when the layer is freed.

This seems to cause memory issues when the layer is freed.

Specifically, there is an issue during the call to CC_SAFE_RELEASE(_texture) within a sprite that uses the texture. In X_CODE, I get a memory error at this point (I’m assuming because i have already freed the texture, but I am not sure). I only get this issue when shutting down my application, which I assume means it comes because the texture is freed when the layer is removed, but before the sprites are freed by the custom garbage collection system.

  1. Why is the sprite trying to release a texture that I created and didn’t give it ownership of?
  2. Is the texture somehow an autorelease object, even though it is not called using a create macro?
  3. Could we use a standard memory management convention, such as smart pointers, rather than all of this custom memory management code? I find this very confusing and hard to wrap my head around when I have to manage the memory vs. the framework doing it for m.

Any thoughts on what I might be doing wrong here?

A few more details.

I have a “Sprite Factory” that creates sprites from this texture on demand.
This sprite factory holds a shared pointer to the texture object.
The sprite factory is contained by the layer using it.
When the layer is freed, the factory is freed.
When the factory is freed, the texture is freed.
This seems to cause problems when the garbage collector frees the sprites that had been using this texture.

OK, I’m finding the TextureCache stuff now…still somewhat frustrating that the above causes issues. Is there a way to use textures without using the built in memory management? Is there a way to make what I described above work? (or should it be working if it is done correctly?)

Switching to factory fixed this problem. Is it possible to manage your own textures/memory when using cocos2d-x? If so, maybe I will understand how better in the future. If not, perhaps this constructors should be private to avoid confusion, forcing people to use the TextureCache functionality.

Regards,

Ben