everytime I tap a button, myfunc is called. Creates a new texture and calls garbage collection. “tex” shouldn’t be collected obviously, but previous “tex” are unreferenced thus should be collected. In each button press, used memory increases about ~1MB
The leak disappears if I call tex.Dispose after Texture2D creation.
Question is… is this an expected behaviour? (must I dispose all unwanted textures manually?) or a bug and the finalizer should do the work?
It might be a threading issue. When you call Dispose() manually, it is called in the same thread context where it was created. If you leave it for the finalizer, it will be called on a different thread.
In all cases, you should never rely on the finalizer. It is there as a backup. You should always call Dispose() yourself on objects that implement IDisposable.
thanks. I was asuming that everything texture-related was fred correctly as with XNA, I must have lots of leaks in all my games
Babinicz: It’d make no difference. The problem is not for the “tex” object running in the same context, but with garbage collected 'tex’s from previous function calls. Calling GC is allowed althought not usually needed in most environments. In example, it’s useful to call GC just before the game is about to be played to avoid GC step in the middle of the gameplay.
I wrote some ‘Console.Write’ in Monogame’s finalizer code and I couldn’t make things clearer to me. Personally I think that monogame finalizer is not liberating the GPU resources (but it can’t, as they’re managed objects), so the finalization code which should wipe the GPU resource should be on SharpDX, and that’s a black box to me.
Anyways I wouldn’t worry much about it. I buy Tom’s comment in #2173 about xoofx’s comment (a bit recursive, I know )
It’s just that I didn’t know, decided to investigate the memory leaks and reached this ridiculous fail sample. Now that it’s known I’ll be extra careful with GPU resources, and problem solved. It’s not hard, just unexpected