question about spritebatch.draw XNA

Started by
4 comments, last by FoxHunter2 15 years, 8 months ago
Hi If I have this piece of code: spritebatch.Begin(); spritebatch.Draw(texture,new Vector2(10,10),new Rectangle (0,0,100,100),color.White); spritebatch.End(); now since this is in the Draw function and it is called 60 times per second, does this mean that in each second there will be 60 new Rectangles and 60 new Vector2 created and so after some time I will run out of memory? I am not sure how this exactly works or are the Rectangle and Vector2 that were created in previous call to Draw replaced by the new ones? Thanks
Advertisement
Yes, but don't worry about it. They don't consume more and more memory. They are released right after they get used. They only have function level scope, and are gone by the time your draw function returns.
I'm sure someone else will give you a lot better explanation.
Vector2 and Rectangle are both structs which means they are value types. This means they get created on the stack and will be destroyed at the end of the method since you aren't holding any reference to them. So no, you will not run out of memory with that code.
thanks
I guess I was a little bit confused since I have some background in C++ and when you use the new keyword there, then things are not destroyed after the function ends.
When you're using structs, "new" doesn't actually mean "make me a new instance on the heap" like it does in C++. It just calls the constructor of that struct and copies the data. In C++ you could also do this, you just didn't use the new keyword. For example take this C++ code:

void SomeFunction(D3DXVECTOR2 vec2){    ...}void SomeOtherFunction(){    SomeFunction(D3DXVECTOR2(5, 10));}


That code does the same thing basically: it calls the D3DXVECTOR2 constructor to create a temporary struct (which is created on the stack, since its temporary) and that value of that struct is copied to be used as the parameter of SomeFunction.

Of course if you're talking about a C# type that's a class and not a struct, then new does create a new instance of that type that takes up memory.

[Edited by - MJP on August 15, 2008 11:13:48 AM]
Quote:Original post by NickGravelyn
Vector2 and Rectangle are both structs which means they are value types. This means they get created on the stack and will be destroyed at the end of the method since you aren't holding any reference to them. So no, you will not run out of memory with that code.


Correct, he will not run out of memory. But I still wouldn't advise him to use this technique all over the project. If the position and rectangle are fixed then they can be declared const (respectively readonly) once in the code.
If they're not fixed, then I would assign the (X,Y)/(X,Y,width,height) properties in the update loop instead of using a new call.

While on a PC this will probably not result in a performance hit, on a XBox 360 the garbage collector does work differently and this code might have a larger impact on performance.

I'm not telling you Nick, you know about this, it's something the OP still should consider changing :)

regards

This topic is closed to new replies.

Advertisement