Converting TIFF to PDF working unexpectedly

@Timpa_elite,

Since it will be difficult for me to replicate the problem I have some questions and advice.

What version of Aspose PDF are you using?
What version of Net are you using?

We have to understand that the error is happening because you are disposing an object which is still being used by a different thread.

Does the error still happen if you comment the following line: myImage.Dispose();

I searched for people who got the exception “Exception Info: System.ObjectDisposedException: Safe handle has been closed.
Object name: ‘SafeHandle’.”

Most of the time is for bad usage of the using block.

Review your references, and your usings because I have the feeling that may be where the issue is in.

Maybe remove the using and let the garbage collector by itself get them when they have on reference.

If you notice you have a problem(Memory leak), somewhere in your code are keeping reference to the objects so they cannot be properly disposed.

I am sorry I cannot tell you exactly where the issue is but I will remove usingd and manual disposes.
Check if your memory usage is getting higher, which means the systems cannot dispose some objects and start from there.

Hi,

I tried to remove all the usings, no success.
Then i tried to comment out most of the code, converted some files to see if it worked and step by step uncomment a line to narrow down where the exception happens.
Finally we managed to locate it, it’s when we set the property:

image.IsBlackWhite = true;

that we sometimes get an exception.

We’re running .NET 6 and the latest version of Aspose.PDF, 23.1.1.

Is this a common exception and is there any fix on the way, or for now a workaround?

@Timpa_elite,

This is not a common exception.

Is the way you made the code. I am not sure how to avoid it.

You can verify if the image is not null before calling that. But again, is very strange.

You need to research why that exception happened in .net as I said.

It is because an object is manually disposed (Something you code) but was been used in another thread.

Can you show me a way to convert Tiff to PDF that you are sure works?

@Timpa_elite,

The conversion you are using seems to be okay, but I would pay attention to how you dispose the object. If I could point out exactly to which line or where exactly is the issue I would let you know right away.

I cannot replicate the issue in my local code. But I did a simple search over the internet to see when this exception happened. Most of the time is by bad implemented using, manually setting variables to null while the object is still in use or manually calling a dispose method of an object while they are still in use.

If I were you I will search for the exception and look at what other programmers did to avoid this issue.
I believe this has nothing to do with Aspose.Pdf, or we would see this issue all over our forum, but how you code the solution.

One more question you need to analyze is(general questions, not necessarily related to the code)

why I am using manually dispose of objects?
is GC not capable of doing it automatically?
If so, where in my code are the references that are not released properly?

If you answer all of those question you will be closer to know how to change your code ot avoid this exception.

Yes,
That’s why i have tried to remove all manual disposing of objects and even all the usings so nothing should be disposed in beforehand.
The only thing that helps in all cases is if i turn of the IsBlackWhite property, isn’t that strange?

@Timpa_elite,

Can you humor me and move that line just after the image is created?

Does still throw the error??

The “DownScaleImageToA4” line?
I’ve tried that before without any success, i tried it again but same thing.

This is the callstack if that’s of any interest to you.
callstack.png (23.2 KB)

@Timpa_elite,

On final thought, I do not know if it will be of any help but maybe stop using

pdf.OptimizeResources(OptimizationOptions.All());

Since this method is obsolete in .Net Core.

I took the time to build a small MVC web, with an input to upload the files.
Then i call an API to convert the Tiff files to PDF.
This is replicating pretty much exactly how our solution works.
Doing this i could replicate the exception, toggling the “IsBlackWhite” still seems to be the solution for me.

Please do try it and see if you get the same behavior.
Open the “TifToPdf” folder and open that solution, then set multiple startup projects.

[timppaa/AsposeConvertTiffToPdfErrorSample (github.com)](https://github.com/timppaa/AsposeConvertTiffToPdfErrorSample)

Please let me know if you get the same behavior as me with this solution.

@Timpa_elite,

Thanks for asking. I think you should have the code you need to make the Aspose parts of your solution work - in terms of more general parts of your code unconnected to Aspose I’m not able to help with those.
But if you feel you still need more help with Aspose-related code then please let me know and I’ll continue to help.

How is this not Aspose-related code?
The exception is being thrown when i set the “IsBlackWhite” flag, have you tried the solution i uploaded?

@Timpa_elite,

Yes, I downloaded your repo and run it locally uploading the picture you uploaded before.

How many times do I have to run the code in order for the exception to happend?

Copy the file, let’s say 50 copies then upload them all and start converting. That should trigger the exception everytime. It differs when the exception happens, sometimes on the fifth file and sometimes on the twentieth etc, even though it’s the same file.
Also make sure the “image.IsBlackWhite” is set to true.

@Timpa_elite,

So after some testing I was able to replicate the error with 20 or 30 images, but noticed that issue was tied to Aspose.Drawing version 22.10. So I downloaded the latest one, Aspose.Drawing 23.2.

I was able to succefully made those files, which prior to this update I was not.

After this change I got an error produced in this library:

C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.14\System.Private.CoreLib.dll.

With this information I will contact the development team.

Can you do me a favor and update Aspose.Drawing, and tell me if you see the same error?

@Timpa_elite
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-53733

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

We managed to find an interesting thing, why the exception doesn’t happen when we run from a test.
When we run from a test the GC is set on workstation mode, but when we call from another client the GC is set as server.
If we turns this flag off:

<ServerGarbageCollection>false</ServerGarbageCollection>

We can maybe work around the problem for now, depending on the memory usage. We also see that alot of memory doesn’t seem to be disposed.

Here’s some more info also:
671AC68C.jpg (274.9 KB)

@Timpa_elite,

I appreciate this information and I will add it to the ticket.