I remember shawn hargraves saying that he didn’t like from file which is what this is basically because of just the scenario you gave. However FromFile is sort of requisite at times and you need to know a little bit about the dangers of it when using it.
Consider the following scenario you create a texture as you did or using from file or set data.
Texture2D t;
// first
t = MouseCursor.FromTexture2D(mouseAnim[i], 0, 0);
lets say you do this later on again but load in a different texture without calling dispose.
// second
t = MouseCursor.FromTexture2D(mouseAnimB[i], 0, 0);
.
then you call dispose.
.
t.Dispose();
What allocated memory does t now refer to ?
t refers to the second texture only now.
So.
How does the first texture get disposed now ? …
Answer is … it doesn’t and you can’t the reference is now dangling in limbo.
So what and were is the reference to the first texture now ?
This may not be spot on technically but practically this is how it is.
The reference is pinned memory basically and the gc can’t reclaim it.
The video card thinks you are using it so its not unloaded there either.
Worse yet its got the reference to t for first and second memory areas.
You no longer have acess to it.
The gc does but it wont touch it.
It’s essentially a semi dangling reference but since its not accessible and can’t be called even by the gc it may not even crash the app or just might do so at a unexpected time or variable time depending on other things going on in the os (Like maybe a Alt Enter or Tab).
Video cards do their own thing the driver has more control over it then the os and (when the card starts to fill up say in the case were you have multiple apps running at once) they can if need be grab the reference they have and clear out memory temporarily then sort of reload it using that reference. Which also means if the second texture you loaded has a different size then the first this could be a app or card crashing event when the reference for the first and the second are the same when they shouldn’t be and there could even be a buffer overflow or underflow into a protected memory area which would probably now days just be a app crashing event too.
All this applys to set data as well. It’s not so bad if you just load a small texture and forget to dispose it before exit but if you did this a crap load then exit the app and reload it over and over it can add up and you can actually tax the card with no way to clear that memory till you restart your computer.