Menu

#4 NullReferenceException when calling Dispose

1.0
closed
nobody
None
2019-04-06
2019-03-20
No

First off, thank you for your efforts creating this utility!!!

I found that while using ZintNetLib to generate barcodes that do not contain text there is a null object exception while disposing of the ZintNetLib. Here are some snippets below to reproduce this issue:

Case 1: QR Code
using (var bmp = new Bitmap(200, 200))
using (var g = Graphics.FromImage(bmp))
using (var zint = new ZintNetLib())
{
zint.CreateBarcode(Symbology.QRCode, "This is a test");
zint.DrawBarcode(g, new PointF(0.0f, 0.0f));
} // Null Reference Exception here ZintNetLib object is disposed

Case 2: DataMatrix
using (var bmp = new Bitmap(200, 200))
using (var g = Graphics.FromImage(bmp))
using (var zint = new ZintNetLib())
{
zint.DataMatrixSquare = true;
zint.EncodingMode = EncodingMode.GS1;
zint.CreateBarcode(Symbology.DataMatrix, "[01]00000000000000");
zint.DrawBarcode(g, new PointF(0.0f, 0.0f));
} // Null Reference Exception here ZintNetLib object is disposed

Both cases have the same StackTrace:
at ZintNet.ZintNetLib.Dispose(Boolean disposing) in c:\Users\Milton\Documents\Visual Studio 2013\Projects\CSharp Projects\ZintNet\ZintNet\ZintNetLib.cs:line 811

I found that for barcodes that do not generate text this never gets set.

A simple fix is to either initialize the value in Init(), or to check for null before calling dispose:

Using the new C# null check operator this is simply:

             mFont?.Dispose();
            textFont?.Dispose();

Or if that is not available in your version of C#:

             if (mFont != null) { mFont.Dispose();
             if (textFont != null) { textFont.Dispose();

Again, many thanks for the utility!

Discussion

  • Milton Neal

    Milton Neal - 2019-03-21

    Hello Brian, thanks for bringing this issue to light.
    I will apply your suggested bug fixs and update the code repository and project files.
    With thanks ... Milton

     
  • Milton Neal

    Milton Neal - 2019-04-06
    • Status: open --> closed
     

Log in to post a comment.