Sign in to follow this  
killan

Texture disposing

Recommended Posts

Hello, i have a method to modify a texture (c#) : mytexture = TextureUtils.TextureChangeColor(mytexture, ...); i pass mytexture as a parameter of the method and the method return me a texture that i place in mytexture in TextureChangeColor i create a tempory texture to receive the result and it's returned it's ok, that's run but i have see a probleme in memory used at each call of this method, the memory used grow, but never decrease so i think to the garbage collector, or to the dispose method of the object texture, but where use it for not destroy the result texture ? i hope you understand my problem, it's difficul to explain in english thanks Killan

Share this post


Link to post
Share on other sites
You should do something like this:

temptexture = TextureUtils.TextureChangeColor(mytexture, ...);
mytexture.Dispose ();
mytexture = temptexture;


You could use a ref parameter too and dispose the old texture inside the method and return the new texture with the same parameter;

But as you replace your old texture with the new one you should think about doing the whole operation in place without creating a new texture.

Share this post


Link to post
Share on other sites
i have tested your answer but it not work

once time it's ok, but it's in a loop and the second time i have "programm error" and the program ended

have you an idea ?

thanks


Killan

Share this post


Link to post
Share on other sites
Sounds like there is another problem somewhere in your code. Can you please give some more information’s about the error that you see? Normally you should get more than a simples “program error”.

Share this post


Link to post
Share on other sites
of course :) thanks, the error is (french version)

La référence d'objet n'est pas définie à une instance d'un objet.
Microsoft.DirectX.Direct3D
Microsoft.DirectX.Direct3D.SurfaceDescription GetLevelDescription(Int32)
at Microsoft.DirectX.Direct3D.Texture.GetLevelDescription(Int32 level)
at grak.TextureUtils.TextureChangeColor(Texture t, Int32 r, Int32 g, Int32 b) in d:\grak\eti\textureutils.cs:line 19
at grak.EtiFonduNoir.Action(Single elapsedTime, Texture[] ETITextures) in d:\grak\eti\etifondunoir.cs:line 33
at grak.grak_noyau.action(Single elapsedTime) in d:\grak\grak\grak.cs:line 388


the code is :

temptexture = TextureUtils.TextureChangeColor(ETITextures[0], IncTotal, IncTotal, IncTotal);
ETITextures[0].Dispose();
ETITextures[0] = temptexture;

in the general program loop, ETITextures[0] is used everytime

is it help you to understand ?

Share this post


Link to post
Share on other sites
I hardly speak French but if I understand it right this is a null reference exception in your TextureChangeColor method (line 19). If you don’t mind can you please post the code of this method? It looks like that you use an texture object that is not valid.

Share this post


Link to post
Share on other sites
The code is very big so i can show you the important part regarding the texture problem

-------------------------------------------------------------------------------
Method that receives the original texture, sends it to a treatment method,
receives the result and returns it
-------------------------------------------------------------------------------
public override void Action(float elapsedTime, Texture[] ETITextures)
{
//On repart d'une image neuve à chaque fois
//Le traitement se fait donc à chaque image

temptexture = TextureUtils.TextureChangeColor(ETITextures[0], IncTotal, IncTotal, IncTotal);
//ETITextures[0].Dispose(); //Pour ne pas cumuler les données en mémoire
ETITextures[0] = temptexture;
...
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
The treatment method for copying texture and return the result

!!!! >>> it's here that we create a new and never disposed texture

-------------------------------------------------------------------------------
public static Texture TextureChangeColor(Texture t, int r, int g, int b)
{
Color colo;
int br, bg, bb;
SurfaceDescription desc = t.GetLevelDescription(0);
SurfaceDescription s = t.GetLevelDescription(0);
Texture tex = new Texture(t.Device, desc.Width, desc.Height, 1, 0, t.Device.DisplayMode.Format, Pool.Managed);

uint[,] data = (uint[,])t.LockRectangle(typeof(uint), 0,
LockFlags.None, s.Width, s.Height);

uint[,] datadest = (uint[,])tex.LockRectangle(typeof(uint), 0,
LockFlags.None, s.Width, s.Height);

for (int i = 0; i < s.Width; i++)
{
for (int j = 0; j < s.Height; j++)
{
colo = Color.FromArgb((int)data[i,j]);
...
datadest[i,j] = (uint)Color.FromArgb(br,bg,bb).ToArgb();
}
}

t.UnlockRectangle(0);
tex.UnlockRectangle(0);

return tex;
}
-------------------------------------------------------------------------------


Regarding ETITextures[0], finaly it's used for render at each loop of the main render loop

ecran_primaire.device.SetTexture(0, ETITextures[0]);



It's difficult to show or send you the code because there are a lot of little programs compiled as DLL to run with a main program.

i hope you can help me with that. thank a lot.


Killan

Share this post


Link to post
Share on other sites
i think there are no solutions in my program structur, it's too cyclic. i search an other way to realize what i want

thanks all

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this