[Solved] Question: Drawing Rectangles and When to Call `Dispose`

While researching how to draw Rectangles in Monogame, I came across this answer and the below code sample:

SpriteBatch spriteBatch;
Texture2D whiteRectangle;

protected override void LoadContent()
{
    base.LoadContent();
    spriteBatch = new SpriteBatch(GraphicsDevice);
    
    // Create a 1px square rectangle texture that will be scaled to the
    // desired size and tinted the desired color at draw time
    whiteRectangle = new Texture2D(GraphicsDevice, 1, 1);
    whiteRectangle.SetData(new[] { Color.White });
}

protected override void UnloadContent()
{
    base.UnloadContent();
    spriteBatch.Dispose();
    
    // If you are creating your texture (instead of loading it with
    // Content.Load) then you must Dispose of it
    whiteRectangle.Dispose();
}

protected override void Draw(GameTime gameTime)
{
    base.Draw(gameTime);
    GraphicsDevice.Clear(Color.White);
    spriteBatch.Begin();

    // Option One (if you have integer size and coordinates)
    spriteBatch.Draw(whiteRectangle, new Rectangle(10, 20, 80, 30),
            Color.Chocolate);

    // Option Two (if you have floating-point coordinates)
    spriteBatch.Draw(whiteRectangle, new Vector2(10f, 20f), null,
            Color.Chocolate, 0f, Vector2.Zero, new Vector2(80f, 30f),
            SpriteEffects.None, 0f);

    spriteBatch.End();
}

Questions:

  1. My first question concerns this section here:

     protected override void UnloadContent()
     {
         base.UnloadContent();
         spriteBatch.Dispose();
         
         // If you are creating your texture (instead of loading it with
         // Content.Load) then you must Dispose of it
         whiteRectangle.Dispose();
     }
    

    When you create a new project in Monogame 3.8, you don’t have UnloadContent created for you.

    My question is, how accurate is the comment in the above section for Monogame 3.8?
    Do I still need to call Dispose for every asset that I create that doesn’t use Content.Load?

  2. The above code snippet works fine with Monogame, but is there a more standard/accepted way of drawing Rectangles in Monogame?

OK, so, it depends on how the asset was created, are you unloading to do something else with the same runnign process or is the app process getting killed?

It’s about memory management, if you didn;t load an asset with content.Load, then it can’t clean that asset up and remove it from memory, you have to do it manualy.

This has nothing to do with drawing the reectangle, this is all to do with loading and unloading assets, for the rectangle, this will be a Texture2D asset.

1 Like

Hmm, I see… I guess it’s good practice to always prepare Dispose statements for one’s non-Content.Load assets then.

Although I’m not really at the stage where I need to Load/Unload assets on the fly (I’m still playing around with simple projects for now), it seemed like a good thing to ask so I know what best practices to use in the future.

I’ll probably ask a new question once I get to a situation where Loading/Unloading assets will be necessary.

Thanks again!

P.S. What about the Rectangle drawing code? Is everything fine with it? :slightly_smiling_face:

1 Like

I don’t see any issue with the draw calls, they look standard at a glance…

1 Like

Thanks! Will now close this question then! :smile: