I have a thread with a XojoScript object running fine. The problem is I’m having trouble getting rid of the script object afterwards.
It appears that I have to clear all properties in the object manually, where I was hoping I could simply do a myXojoScript.Reset and myXojoScript = Nil for example.
This approach does not return the memory taken by the object though. I also tried to kill the whole thread object that runs the script, but although that does finish (?), the memory problem stays.
Am I missing something here or do I indeed have to clear all properties of myXojoScript before it destructs and frees the memory?
Is it possible that there are other objects in the program that have references to the XojoScript Object? That can prevent the destruction of the Object and the recovery of the memory. Apologies in advance if you have already checked for this scenario.
No prob, thanks for your input!
But yeah, I’ve already double-checked that. Only the thread that runs the script object has a reference…
How are you creating the XojoScript instance ?
In the run event of the thread I do:
atScript = New AirTableScript
Which is a property of the thread and a type from super XojoScript in the app.
Is that what you mean?
Since XojoScript does not have a close method, I suspect it cannot be destroyed that easily.
Threads do not have a close method that would remove them either.
I would try instead of attaching atScript to a module or to the window, to attach it to a new window (if necessary invisible) or a containerControl. Or even dragged on that CC or window. Then when necessary closing the CC or window should destroy whatever is on it, and with some luck the XojoScript as well.
Hey, you might as well put the thread there as well.
Since I don’t have a GUI, it would be a module. Never used that before though. I’ll see if I can build a prototype…
You cannot destroy a module. It is not an instance.
Ah. I see. I also can’t have my events it seems. Makes sense from a GUI point of view though.
Found it. In the Constructor of the XojoScript object I initialized a couple of local properties that never got Nilled again.
Totally forgot I ever used that mechanism…
Still strange IMHO that the thread object that hosts the script object can destruct with some of the script object still in memory. Or am I misunderstanding something?
I am not sure killing the thread really destructs it. More probably, it simply stops it.
Well, the Destructor method fired for the thread, but initially not for the script inside that object.
I can trust the Destructor as a signal that I cleaned up nicely, right?
Sounds like another Feedback then.
But first some sleep.
What are these properties you have to nil for the script to destruct? It sounds like there could be a circular reference somewhere and this breaks that.
Have you tried to put a destructor on the XojoScript ? That could give you additional information.
With some fresh eyes it turned out to be the ultimate circular reference indeed. In the constructor I did:
Self.context = Self
Which I had to clear from the outside after finishing the script for the XojoScript object to destroy.
I used a destructor on both the thread and the script object. That’s how I found out that the thread ended fine but the script object inside it did not.
I’m still not sure if that’s normal behavior. I figured for the thread object to destruct, the included xojoscript would have to be gone too… Is this a bug?
@Michel Bujardet: did you mean additional information besides it not destructing?
You found the reason for the bug. Indeed, I think it is normal that the thread won’t destruct with something alive in there. What is of concern, is that it might create another bug when the method ends, as it will stay there hanging around.
There is such a bug report in Desktop where if a thread was still running when the window closed, it crashed the entire program.