Closing StarDesktop by macro from any document leads to crash of LO

I’m creating a database, which should be closed completely when last form has been closed. No problem to do this when other documents are open, But when doing this with Base document only it leads to a crash of LO.

Tried this directly in “My Macros and Dialogs”:

SUB TestClose
	StarDesktop.Terminate()
'	document   = ThisComponent.CurrentController.Frame
'	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'	dispatcher.executeDispatch(document, ".uno:Quit", "", 0, Array())
END SUB

You also see the alternative, which is given by the menue. Will quit LO without any problem when executed by menue. Will lead to a crash when executed by a macro.

My system:
Version: 24.2.4.2 (X86_64) / LibreOffice Community
Build ID: 51a6219feb6075d9a4c46691dcfe0cd9c4fff3c2
CPU threads: 6; OS: Linux 6.4; UI render: default; VCL: kf5 (cairo+xcb)
Locale: de-DE (de_DE.UTF-8); UI: de-DE
Calc: threaded

Note: Sometimes, under Linux, it won’t show any crash. But I couldn’t start LO directly after I have closed the database file. Looked for open process and found LO is still activ for a while, then closed.
Windows-user reported it will crash every time …

See also: Automatisches Schließen einer Datenbankdatei und ggf. des gesamten Desktops

If this means that you have the Basic IDE open and try to run the macro (placing the cursor in its range and hitting F5 e.g.) this can’t work because the IDE can’t be closed while a macro is running.
During my test with LibO V24.2.4.2 under Win 10, however, this was correctly stated by a message, and didn’t cause a crash.
Calling the macro via the menu from an example .odb (hsqldb or firebird) it ran as expected without leaving traces.
The macro also didn’t cause a crash when run from an “empty” (no open document) desktop, but didn’t close it.

No, it is only saved there. I closed the IDE, opened a new Writer document and then started the procedure.
I did the same from a simple database file.
This has been reported first to me from a Windows desktop. So I opened systemmonitor here under KDE to see, what is happening. And, indeed, soffice is running after closing the database file this way. And when closing a new Writer document the desktop will show “LibreOffice has crashed”.

StarDesktop.terminate() returns a boolean, therefore it can not be used in document embedded code because you unload executing code before it can return a result value. In other words, you crash the program.

it finished. This is independent of whether or not a result is prepared for output in Basic. Subroutines declared using Function or Sub are equivalent insofar.
Testing with

Function TestClose()
TestClose = StarDesktop.Terminate()
End Function

also yields the mentioned positive (no crash) result in my tests.

The word in was the reason for my question if the IDE was open, but it was clear that the questioner didn’t try to run the macro from a module contained in the document.

Regarding the OQ the only relevant difference between “working” and “crash” was the document type.

This may be due to a not reported fact: The used database document may work with a non-embedded database. Then there may be unclosed connections. Lacking related experience I can’t investigate this suspicion…

This problem has nothing to do with databases.
I have saved the procedure in “My Macros & Dialogs” and closed the macro editor.
Then File → New → Textdocument
Then Tools → Macros → Run Macro → TestClose
KDE (Linux) shows a crash.
In most cases: soffice is still running in background.
This won’t happen, when directly pressing File → Exit LibreOffice.

By the way: For closing a document only I set oDoc.Close. Only for closing LO when no other frame is still open I set StarDesktop.Terminate()

Meanwhile, I got hold of a Windows PC. I can not reproduce the problem, no matter where I store the termination macro. LibreOffice vanishes from the task manager anyway.

I obviously don’t understand. I’m out now.

Seems a little be confusing, because my English isn’t very well.
@Lupp : Set “Base document” as an example. I’m working with Base here. It is an example for every Document. Try it with Writer. Gives the same buggy result. If the writer document I use is the last (or only opened) document and I try “oDoc.Close(True)” LO keeps running in the background.

In my current construction I will close a database file with the macro.
Closing the file runs with oDoc.Close(True).
Closing the file runs well if other files where opened (like Writer, Calc …)
Then there appeared problems under Windows when only the database file is open. The file itself hangs.
I noticed there is a process almost running under Linux when trying this. Wondered why LO could be started so fast after I have shutdown LO this way.
So I looked for a different possibility to shut down whole LO when only the database file (and here you could set “Writer” file, “Calc” file and so on) is opened.
I tried with StarDesktop.Terminate(), as @Villeroy has written in German post. Seems to work well here, but leads to a crash under Windows for other persons. Then I recognized: Sometimes I couldn’t start LO directly after I have closed it this way on my Linux system. So I looked at the open processes and recognized: LO is still working in the background.

And this is the problem: LO won’t close well with any of the methods I could find. I could only close a document by .uno:CloseDoc and .uno:CloseWin and get the StarDesktop and the close the desktop without macro. I can’t close the last open document with .uno:Quit or StarDesktop.Terminate() without getting problems with crashing or running soffice in the background …

Do you know their config? Win10/11 LO- Version, Quickstarter active…
.
Will try later, if my employers machine crashes, but I’m at LO 7.4 there with Win10pro.

@Wanderer : Have asked them for their config.

LOclose.odt (12.9 KB)
Tested now with this file.
Close all other documents before you open the file. The Writer file should be the only opened window of LO.
Press the button.
LO closes, couldn’t detect any process here with LO 24.2.4.2 under OpenSUSE Linux.

Now copy the macro from the file to “My Macros …” (rename the procedure).
Save the macro.
Close macro editor so only the Writer file is opened.
Execute the macro Tools → Macros → Run Macro → My Macros → …
LO closes, KDE reports a crash here and the process for LibreOffice is still running.

This file closes successfully on my Windows system, both from the document’s library, and from “My Macros” (with the macro renamed). I also tried with a Linux version of LibreOffice:
Version: 24.2.4.2 (X86_64) / LibreOffice Community
Build ID: 420(Build:2)
CPU threads: 24; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (C.UTF-8); UI: en-US
Ubuntu package version: 4:24.2.4~rc2-0ubuntu0.22.04.1~lo1
Calc: threaded
and it also closed successfully both times, with same conditions. Note that after copying the macro to My Macros, I actually exited LibreOffice completely, before opening it again and executing the macro from there, to avoid forgotten IDE.

It is possible that a dialog window may create problems in specific integrations (KDE?), e.g. resulting in the window’s parent destroyed first, and the child window becoming orphaned. Needs specific conditions tested (I don’t use KDE).

Seems to be a special buggy behavior of KDE. Doesn’t solve my main problem (closing a database with this), but this might be another thread.
Don’t know how to close such a thread.

Did you mind the nasty “quick-starter”? This is the first thing I remove from a Windows installation.

Seems I got what leads to a crash: I want to terminate the desktop with an event of the document. Have done this in the attachment by Customize → Events → 'Modified' status has changed. Makro will save the document, shows a message and terminate Star Desktop.
If I change anything, type a char or something, the macro will be executed. LO crashes and leaves a process in task manager.
If I only press the button, connected to the same procedure, it will work without any problems.
LOclose.odt (13.1 KB)