When I resize the game window and the viewport's height becomes 0, GC disposes of spritebatch, I think. Is this what happens? How do I prevent this?
Asked
Active
Viewed 527 times
3
-
1Now I know how to prevent this, still it would be interesting to know how spritebatch gets disposed of. – user1306322 Oct 04 '12 at 17:37
-
1Just to be clear, the GC doesn't dispose of the `SpriteBatch`. I just ran a little test and it seems that XNA is *supposed* to prevent resizing down to zero (weird that it doesn't for you). Resizing can cause the graphics device (and all its resources) to be re-initialized. If it fails to re-initialise because the size of the window is zero, then `SpriteBatch` ends up in more-or-less the same state it would be in if it were disposed. – Andrew Russell Oct 05 '12 at 07:57
-
I tried this on an empty new project and resizing causes this error, but none of my older projects behave like that. Well, I guess there's really no need to resize to zero, so I could set minimum size like in the answer. – user1306322 Oct 05 '12 at 09:57
2 Answers
3
Form gameForm = (Form)Form.FromHandle(Window.Handle);
gameForm.MinimumSize = new System.Drawing.Size(800, 600);
Short and sweet!

Joe Shanahan
- 816
- 5
- 21
-
Nice. But this requires `using System.Windows.Forms;` or `using Forms = System.Windows.Forms;` to avoid unwanted ambiguity. – user1306322 Oct 04 '12 at 17:36
-
Still, this may not work for all cases with the same values, it has to be compensated for the size of window theme borders. Guess this should be done using `form.ClientArea`. – user1306322 Apr 26 '13 at 12:22
1
I had this same problem and some quick debugging shows that XNA calls UnloadContent and then LoadContent again to reinitialise resources; my guess it it loses the GraphicsDevice or something, hence the reload.
Creating things pertaining to the GraphicsDevice in LoadContent solves this problem.
Now, for this particular case, setting a minimum size is a good idea, but I don't know if this is a portable solution between Xbox and Windows. However, there might be other situations in which something similar occurs, where UnloadContent is called, so it's probably best to adhere to this practice.

MicroVirus
- 5,324
- 2
- 28
- 53