StackOverflowException issuse using Aspose.words

Hi,
We’re having issues when we convert an .rtf document to .docx using Aspose.words (We’re using the last version: 18.1) in some contexts, When generating the .docx Aspose.words file the following exception:

Process is terminated due to StackOverflowException.

More specifically we have a GUI application built using C++ Builder XE3 from Embarcadero, which then calls a MSVC C/C++ library that then call a CLR wrapper which handles all the calls to Aspose

I’ve reproduced the issue in a set of simplified standalone applications and libraries, in the attached zip file.

The workflow is
1- Project1.exe (the C++ Builder project)
2- AsposeCLRLib.dll (a Visual Studio C++ project that is CLR compatible and compiled using stdcall calling convention that exports 1 function and uses the next item)
3- AsposeLib.dll (A .NET library that acts as a wrapper and calls the Aspose libary)

The issue is only when a C++ Builder XE3 project is involed in the call sequence; to reproduce the issue the project in “EmbarcaderoRadStudioXE3Project” must be used (or the resulting Project1.exe)

Note: In the zip attached, the aspose.words dll version 18.1 is not included because it was too big to include it

Thank you for your help

IssueStackOverflowUsingAsposeWords.zip (136.3 KB)

@alexpezo,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16392. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

(Post removed, replied to the wrong thread)

@RanchlerRabbit,

Thanks for sharing the detail. We will inform you via this forum thread once there is any update available on this issue. Please let us know if you have any more queries.

@RanchlerRabbit,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. The problem occurs somewhere inside C++ or Embarcadero because we are using Double.NaN value in our code. You can reproduce the same problem with any other .NET assembly which uses Double.NaN inside the code.

namespace DoubleNan
{

  public class TestClass
  {
	public TestClass() { }

	public void TestMethod()
	{
		Console.WriteLine("Start1");
		double test = Double.NaN;
		Console.WriteLine("End1");
	}
  }
}

Please create a wrapper class and register it using regasm.exe utility. Try to use this assembly using C++ in Embarcadero RAD studio. You will get the same exception like upon using Aspose.Words. The same assembly works fine in VB via COM.

Moreover, please refer to the following article. Hope this helps you.
Exception in managed code results in Access Violation crash

Hi, the original poster is a colleague of mine and I helped him create the small test setup that reproduces the issue outside our application

What you’re saying doesn’t make much sense to me, what we’re getting from Aspose is a stack overflow and not an access violation. And we’re not using double or Decimal values anywhere in the solution we provided.

You might want to have a look at CELLSNET-44893 since we had a somewhat similar issue in the past with Aspose.Cells, where an exception would only be thrown when the process was created by an application compiled through C++ Builder XE3 just in case the root cause of the issue and associated fix might be similar. (it might not be but it’s probably worth having a quick look)

Also note that our application is single threaded, and we’re not appending documents, we just use a string containing an RTF representation of our file (we have a custom C++ framework already in place that generates the RTF file content into an std::string at runtime from a template to replace names, addresses, etc…) the main reason we’re using Aspose here is to save the file to DOCX instead of RTF, like we used to, because of disk space concerns. (we’re creating thousands of files). We’re willing to adjust the way the DOCX the file itself is being saved if needed (i.e.: the calls to Aspose) but we’re limited in what we can do with the source RTF content itself (the content or format of the source file cannot be changed if it impacts the way the end results is displayed).

Also note that the same process does work totally fine for other much bigger files, I don’t know why Aspose is having issues with the one we provided here. If you just replace the B0014F68.rtf file we provided with another one it usually works fine.

I’ve simplified the whole set of projects a little bit to keep only a minimal amount of code in the end we have
1- The Embarcadro project, which calls a single function of AsposeLib.dll, the embarcadero code is minimal and doesnt do any other processing other than calling the external function
2- AsposeCLRLib.dll : Reads the B0014F68.rtf file into a memory stream and sends it to the C# dll (AsposeLib.dll)
3- AsposeLib.dll : Saves the content of the memory RTF buffer to DOCX

Here is what I’m seeing on my end

D:\CompiledAndReadyToRun>Project1.exe
Loading AsposeCLRLib.dll
GetProcAddress for AsposeTestExtC
Calling aspose CLR lib wrapper
AsposeLib.dll → AsposeTestExtC() START
Reading B0014F68.rtf to MemoryStream
Calling AsposeCSharpClass->ConvertToDocxFile(RtfInputStream)
C# Function ConvertToDocxFile Start
Aspose Version Info: 18.1
Loading content of the B0014F68.rtf file into a new Document
Calling doc.Save(@“B0014F68.docx”, SaveFormat.Docx)

Process is terminated due to StackOverflowException.

And to be clear here is the C# code that’s crashing on doc.Save

    static public void ConvertToDocxFile(Stream inputStream)
    {
        Console.WriteLine("C# Function ConvertToDocxFile Start");
        Console.WriteLine("Aspose Version Info: " + Aspose.Words.BuildVersionInfo.Version);

        MemoryStream outputStream = new MemoryStream();
        LoadOptions loptions = new LoadOptions();
        loptions.LoadFormat = LoadFormat.Rtf;

        inputStream.Seek(0, SeekOrigin.Begin);

        Console.WriteLine("Loading content of the B0014F68.rtf file into a new Document");
        Document doc = new Document(inputStream, loptions);

        Console.WriteLine("Calling doc.Save(@\"B0014F68.docx\", SaveFormat.Docx)");
        doc.Save(@"B0014F68.docx", SaveFormat.Docx);

        Console.WriteLine("C# Function ConvertToDocxFile End");
    }

Again, if you replace the provided B0014F68.rtf file with another file it works fine so Aspose is crashing on that specific file I don’t know why, but it makes me believe the issue is in Aspose and not the other way around.

I’ve removed everything I could from the file while still reproducing the issue, please have a second look at the issue.StackOverflowSimplified.zip (123.4 KB) (note Aspose.Words.dll was removed since it’s to big to upload here)

@Luc_Charbonneau,

Thanks for your inquiry. We have logged this detail in our issue tracking system. We will inform you via this forum thread once there is any update available on this issue. We apologize for your inconvenience.

@Luc_Charbonneau,

Thanks for your patience.

We have managed to find out the main reason of this problem and it is not on the our side. The problem occurs because we are using simple double.MaxValue value in our code. This problem can be reproduced with any other .NET assembly which uses Double. MaxValue inside the code.

How to reproduce:
Create simple .NET Class Library, with some test method, inside the method set Double test = Double.MaxValue. Then try to use this simple Class Library inside your application in the same way as you are using Aspose.Words.dll.

namespace DoubleMax
{
    public class TestClass
    {
        public TestClass() { }
 
        public void TestMethod()
        {
            double test = double.MaxValue;
 
            Debug.WriteLine("Simple double.MaxValue is used!");
        }
    }
}	

public class AsposeCSharpClass
    {
        static public void ConvertToDocxFile(Stream inputStream)
        {
            Console.WriteLine("C# Start");
 
            // Try to use this simple Class Library
            TestClass test = new TestClass();
            test.TestMethod();
 
            Console.WriteLine("C# End");
        }
    }

“StackOverflow” exception will be thrown. The same assembly works fine if you remove double.MaxValue. So this problem is not related to Aspose.Words.

Thank you for the additional information, I still don’t understand why this only occurs with that specific RTF document but we’ll work around the issue by creating a new process to handle the conversion through a separate MSVC executable to bypass any issues that might relate to Embarcadero since we’re unlikely to be able to get the compiler fixed. Thanks again for pinpointing the source of the issue.