Jump to content
  • Advertisement
Sign in to follow this  
Metzler

[.net] Strange Bug in VS.NET Compiler?

This topic is 5114 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

Hey, I've got a "funny" bug and do not know, what to do with it. First the code:
		public bool RenderSprite(int spritenum, int pos_x, int pos_y, Rectangle portion, float scale)
		{
			if (SpriteDevice == null) 
			{
				OutputDebug(new Exception("SpriteDevice is null"));
				return false;
			}
			try
			{
				Rectangle temp = portion;
				if (scale != 1.0f)
				{
					temp.Height = (int)(temp.Height * scale);
					temp.Width = (int)(temp.Width * scale);
				}
				Texture temp2 = (Texture)Textures[spritenum];
				if (temp2 == null)
				{
					OutputDebug(new Exception("Texture @ Index " + spritenum.ToString() + " is null!"));
					return false;
				}
				//int i = 0;
				SpriteDevice.Draw2D(temp2, portion, temp, new Point(pos_x, pos_y), Color.White);
				return true;
			}
			catch (Exception ex)
			{
				OutputDebug(ex);
				CheckforReset();
				return false;
			}
		}

	public bool RenderSprite(int spritenum, int pos_x, int pos_y, Rectangle portion)
		{
			return RenderSprite(spritenum, pos_x, pos_y, portion, 1.0f);
		}

The problem is: When i call the upper method, no Texture will be displayed. If i delete the outcomment-tags in front of the int i = 0; above the Draw2D method, it does draw the texture! When i now call the overwritten second method (that does nothing else but calling the method above), it will display no texture! BUT: If i outcomment the int i = 0; and call the second method again, it will paint the texture. Anyone any idea, why?? Thanks for your help! This is driving me nuts... Metzler

Share this post


Link to post
Share on other sites
Advertisement
Do you actually use i? The compiler should give a warning about it being unused, but I agree that it certainly is a curious problem.

Share this post


Link to post
Share on other sites
My guess would be that some sort of stack corruption is going on, if the compiler even decides to build "i" into the program. A rebuild maybe?

Just for fun, try setting i to temp.Height or temp.Width and see if anything happens.

Share this post


Link to post
Share on other sites
No, i do not use i. Thats one of the funny things :)
(actually i got it to work by completely killing the scale part of the code, i didnt use it anyway ;)).
Another really funny thing was: I had about 3 calls to the render-function. So, depending on which "configuration" i used, it sometimes showed the bitmap, sometimes didnt. The Bitmap that was to be shown was the first one in the render queue. If i sat the Bitmap to the last position in this queue, it suddenly rendered the bitmap, depending on the position, my mouse was at, although there was no relationship btw. the variables, i used for the mouse pos and the position, i wanted the bitmap to be drawn at... Never seen anything like that b4...
Felt kind of a flight-sim: When i moved the mouse upwards, the bitmap went down, when i moved the mouse to the left, the bitmap went right etc.... Really really strange...

Share this post


Link to post
Share on other sites
This sounds very odd. If this was a compiler bug, the sheer number of regression tests we have should have caught it I think. Draw2D takes a pointer for its 4th parameter right (the one you're creating an object on the heap for)? You can just pass the constructor, and it gets created on the stack instead. I really don't know what's going on. What version of VS are you using?

Share this post


Link to post
Share on other sites
Visual Studio .NET 2003 on Windows XP SP2.
I yesterday installed the .NET 1.1 SP1. Didnt help...
If you want, i can send you the project (just for the fun of it ;))
Perhaps you can tell me, where the problem is...

Edit: Setting i to temp.Width seemed to kill the "flight-sim" behavior...
@CpMan: Stupid question, but what do you mean with just passing the constructor?

[Edited by - Metzler on October 12, 2004 4:55:19 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Metzler
Hey,

I've got a "funny" bug and do not know, what to do with it.
First the code:


SpriteDevice.Draw2D(temp2, portion, temp, new Point(pos_x, pos_y), Color.White);



Sure, send it to me and I'll try it later tonite if I get a chance.

When you call Draw2D, you pass a point object to it by calling "new Point(pos_x, pos_y)". This creates a new Point object on the heap and you'll need to "delete" it later. Furthermore, the signature of your Draw2D should be Draw2D(...,...,..., pointer, ...). You can instead, pass it like this:


SpriteDevice.Draw2D(temp2, portion, temp, Point(pos_x, pos_y), Color.White);


This creates the Point istance on the stack instead, and you won't have to worry about memory leaks because it will get automatically destroyed when it goes out of scope.

Share this post


Link to post
Share on other sites
Quote:
Original post by CpMan

SpriteDevice.Draw2D(temp2, portion, temp, Point(pos_x, pos_y), Color.White);


This creates the Point istance on the stack instead, and you won't have to worry about memory leaks because it will get automatically destroyed when it goes out of scope.

It's C#, so you have to use new. It won't even compile without. Don't worry, the point will be created on stack despite the "new". "new" is used with value-types for uniformity.

Regards,
Andre

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!