Sign in to follow this  

question about spritebatch.draw XNA

This topic is 3411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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