Cdocbuilder.dispose() in Spring Boot

Hello.

I want to make changes to a Word file using CDocBuilder and save it to memory. The Word file is edited and saved to memory. However, second time the appendToDocument() method called, after CDocBuilder.dispose() method it throws the following error.

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007fff359a773d, pid=5264, tid=16648

JRE version: Java™ SE Runtime Environment (17.0.11+7) (build 17.0.11+7-LTS-207)
Java VM: Java HotSpot™ 64-Bit Server VM (17.0.11+7-LTS-207, mixed mode, emulated-client, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)
Problematic frame:
C [doctrenderer.dll+0x42773d]
No core dump will be written. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as:
C:\Users\user\Desktop\sesda-4\hs_err_pid5264.log

If you would like to submit a bug report, please visit:
Crash Report
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.

Disconnected from the target VM, address: ‘127.0.0.1:53371’, transport: ‘socket’

I’m using Spring Boot. My code is as follows:

public void appendToDocument(String filePath, String newText) {
    try {
        CDocBuilder.initialize("C:/Program Files/ONLYOFFICE/DocumentBuilder");
        CDocBuilder  builder = new CDocBuilder();

        int isFileOpened = builder.openFile(filePath, "");
        System.out.println("DOCX açıldı: " + isFileOpened);

        CDocBuilderContext context = builder.getContext();
        CDocBuilderValue global = context.getGlobal();
        CDocBuilderValue api = global.get("Api");
        CDocBuilderValue document = api.call("GetDocument");

        CDocBuilderValue newParagraph = api.call("CreateParagraph");
        newParagraph.call("SetSpacingAfter", 1000, false);
        newParagraph.call("AddText", newText);

        CDocBuilderValue[] paragraphs = { newParagraph };
        CDocBuilderValue content = new CDocBuilderValue(paragraphs);
        document.call("InsertContent", content);

        builder.saveFile("docx", filePath);
        builder.closeFile();
        CDocBuilder.dispose();
    } catch (Exception e) {
        System.err.println("Exception is: " + e.getMessage());
        e.printStackTrace();
    }
}

I want to know if this method → CDocBuilder.dispose() should be called every time after operations on the file are completed?

Hello @ayla

Documentation on the Dispose method states:

Unloads the ONLYOFFICE Document Builder from the application memory when it is no longer needed. Generally, there is no need to dispose JS before exiting the process, it should happen automatically. It should only be used if the process needs the resources taken up by JS.

Generally, yes, if several files are being processed, then you should call Dispose after each file.