In LibreOffice Calc, how do I change through LibreOffice Basic the value of a cell with an event listener set to it without crashing the program?

I am trying to create two tables which mirror changes made to any of them to one another automatically.

To that end, I added event listeners which are triggered when the cells of these tables are edited by the user.

Unfortunately, editing one of the tables causes LibreOffice to crash, even though the changes are indeed reflected correctly, as seen upon reopening the file.
I thought the crash might be due to a never-ending circular reference, but it still crashes after it has been made non-circular (by commenting out the relevant parts of the code so that changes are reflected only one way rather than both ways).
I noticed the code worked fine when writing to a cell that didn’t have an event listener set to it.

How can I write to one of the cells with event listeners set to them without causing LibreOffice to crash?

You may want to download the following file.

live_mirrored_tables.ods (24.5 KB)

Please run Main and then try editing the cell C3 of the Planning sheet. The arbitrary string “C” should be written in the cell C4 of the Services sheet.

Here is a simplified version of the code :

REM  *****  BASIC  *****

const SERVICESSHEET_NUMBER = 2
const SERVICESSHEET_SERVICES_COLUMN = 2

Type cellStruct
	columnNumber As Integer
	rowNumber As Integer
End Type



Sub UpdateServicesSheet(editedCell As cellStruct, newValue As String)
	
	Dim oSheets
	Dim servicesSheet
		
	oSheets = ThisComponent.getSheets()
	servicesSheet = oSheets.getByIndex(SERVICESSHEET_NUMBER)

	servicesSheet.getCellByPosition(SERVICESSHEET_SERVICES_COLUMN, 3).setString(newValue)
	
End Sub


Private oListener, cellRange as Object

Sub AddListener
	Dim sheet, cell as Object

	sheet = ThisComponent.Sheets.getByIndex(0)  'get leftmost sheet
	servicesSheet = ThisComponent.Sheets.getByIndex(2)

	cellRange = sheet.getCellrangeByName("C3")
	oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")  'create a listener
	cellRange.addModifyListener(oListener)  'register the listener


	cellRange = servicesSheet.getCellrangeByName("C4")
	oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")  'create a listener
	cellRange.addModifyListener(oListener)  'register the listener
End Sub

global CircularReferenceAllowed As boolean

Sub Modify_modified(oEv)

	Dim editedCell As cellStruct
	Dim newValue As String
	
	editedCell.columnNumber = 2
	editedCell.rowNumber = 2
	

	If CircularReferenceAllowed Then
		CircularReferenceAllowed = false
		UpdateServicesSheet(editedCell, "C")
	End If

End Sub

Sub Modify_disposing(oEv)
End Sub

Sub RmvListener
	cellRange.removeModifyListener(oListener)
End Sub


Sub Main

	CircularReferenceAllowed = true
	AddListener

End Sub

Crossposted to :

Attach your test-file here, instead linking to G…drive