We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Random NullReferenceException on AddStamp

Hi,
I’m getting random exceptions while adding a text stamp on a PDF document.
I say “random” as in flacky parallel unit tests because most of the times those tests pass but I cannot figure out why they randomly fail.

This is the stack trace that I get from Aspose.PDF 22.4:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Aspose.Pdf.Text.TextSegmentCollection.#=ze$KTyoanEEhe.get_Current()
   at Aspose.Pdf.TextStamp.Put(Page page)
   at Aspose.Pdf.Page.AddStamp(Stamp stamp)

With 22.5 I have the same exception, albeit with a different encrypted location:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Aspose.Pdf.Text.TextSegmentCollection.#=zzMwSyrvK2gaP.get_Current()
   at Aspose.Pdf.TextStamp.Put(Page page)
   at Aspose.Pdf.Page.AddStamp(Stamp stamp)

This is (roughly) the code that I’m using:

var outputStream = new MemoryStream();
using (Document inDoc = GetAsposeDocument(inputDocument)) // new Aspose.Pdf.Document
{
	var firstPage = inDoc.Pages.FirstOrDefault()
		?? throw new InvalidOperationException(...);
	var stamp = new TextStamp(textToWrite, new Aspose.Pdf.Text.TextState
	{
		FontSize = fontSize, // 11
		ForegroundColor = Color.FromArgb(argbColor.R, argbColor.G, argbColor.B),
		Font = Aspose.Pdf.Text.FontRepository.FindFont("Times", Aspose.Pdf.Text.FontStyles.Regular, ignoreCase: true),
	});	
	switch (orientation)
	{
		case PdfStampOrientation.Left:
			stamp.VerticalAlignment = VerticalAlignment.Center;
			stamp.HorizontalAlignment = HorizontalAlignment.Left;
			stamp.RotateAngle = 90;
			stamp.LeftMargin = 20;
			break;
		case PdfStampOrientation.Right:
			// ^ Likewise, error happens in either case...
			break;
		default:
			// ^ Likewise, error happens in either case...
			break;
	}
	firstPage.AddStamp(stamp); // <===== HERE THROWS THE EXCEPTION
	inDoc.Save(outputStream);
}

I think that this is related to some multithread access because tests are run in parallel, but this kind of issue takes a long time to diagnose without some internal knowledge of what the library is doing.

I won’t ask you to try to reproduce the issue but I’d like to have some hint on what exactly is breaking in the library in order to narrow down the possible problem on my end before submitting an example that is sistematically broken.

Since the dll is encrypted and the exception does not have a message can you please share some hypotesis on what is going wrong?

Thanks in advance.

@usernameisalreadyinuse

The only thing you need to make sure is that always use separate Document instance per each thread.

If you still face problem, please attach the following resources here for testing:

  • Your input PDF document.
  • Please create a simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello,
as I said before, this is random and I cannot reproduce it in a reliable way.

I never reuse a Document across threads and the input file does not matter, it breaks even with a brand new document per thread.

Anyway, after 4 hours of debugging I have put together a test that fails 30%+ of the runs:

Keep in mind that I have a 4 Cores (8 threads) CPU so the number 20 can be different for you.

[TestMethod]
public void TestMultiThread()
{
	var beginWork = new ManualResetEvent(false);
	bool failed = false;
	void doStamp(object state)
	{
		int cnt = (int)state;
		beginWork.WaitOne();
		Console.WriteLine($"Thread #{cnt} started.");

		byte[] docWithOneEmptyPage;
		using (var doc = new Document())
		using (var ms = new MemoryStream())
		{
			doc.Pages.Add();
			doc.Save(ms);
			docWithOneEmptyPage = ms.ToArray();
		}
		var stamp = new TextStamp($"Hello World {cnt}!");
		using (var inputStream = new MemoryStream(docWithOneEmptyPage))
		using (var inDoc = new Document(inputStream))
		{
			var firstPage = inDoc.Pages.FirstOrDefault() ?? throw new InvalidOperationException("NO PAGES");
			try
			{
				firstPage.AddStamp(stamp);
				Console.WriteLine($"Thread #{cnt} stamp added.");
			}
			catch (NullReferenceException oEx)
			{
				failed = true;
				Console.WriteLine($"Thread #{cnt} failed: " + oEx.Message);
			}
		}
	}
	List<Thread> threads = Enumerable.Range(0, 20)
		.Select(i =>
		{
			var t = new Thread(new ParameterizedThreadStart(doStamp));
			t.Start(i);
			return t;
		})
		.ToList();
	beginWork.Set();
	threads.ForEach(d => d.Join());
	if (failed) Assert.Fail();
}

This is a sample of the output for one run:

Thread #11 started.
Thread #16 started.
Thread #9 started.
Thread #19 started.
Thread #17 started.
Thread #5 started.
Thread #6 started.
Thread #8 started.
Thread #18 started.
Thread #10 started.
Thread #2 started.
Thread #7 started.
Thread #3 started.
Thread #15 started.
Thread #0 started.
Thread #4 started.
Thread #12 started.
Thread #13 started.
Thread #14 started.
Thread #1 started.
Thread #15 failed: Object reference not set to an instance of an object.
Thread #11 stamp added.
Thread #13 stamp added.
Thread #1 stamp added.
Thread #16 stamp added.
Thread #5 stamp added.
Thread #19 stamp added.
Thread #3 stamp added.
Thread #10 stamp added.
Thread #6 stamp added.
Thread #9 stamp added.
Thread #0 stamp added.
Thread #12 stamp added.
Thread #8 stamp added.
Thread #2 stamp added.
Thread #4 stamp added.
Thread #18 stamp added.
Thread #14 stamp added.
Thread #7 stamp added.
Thread #17 stamp added.

@usernameisalreadyinuse

We have logged this problem in our issue tracking system as PDFNET-51834. We will inform you via this forum thread once there is an update available on it.

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFNET-51834) have been fixed in Aspose.PDF for .NET 22.8.