hi,
What will happen when I reference a form element in a global variable:```
globals.xxxxx = form[yyyy].elements[zzzzzz];
and the form is unloaded/uncached because mare than 30 other forms have been requested after the form was last shown? (I think Servoy caches 30 forms, the number might be wrong, but then the point still remains)
Will this cause errors? Will I have a reference to an abject that doesn't exist anymore? will it continue to work? Or won't the form be uncached at all?
Any thoughts appreciated. I don't have the ability to test this, because my solution doesn't have the 30 forms yet, but if this would be a problem, I have to think of another way of doing what I want to do.
Paul
why are you storing a element in a global?
(this must be a media type of global ofcourse)
But you shouldn’t keep track of elements this way.
just store the yyyyy,zzzzzz somewhere… So that you can relookup the element when needed.
globals.xxxxx = new Array(yyyyy,zzzzzz);
form[globals.xxxxx[0]].elements[globals.xxxxx[1]]
Storing the reference to an element in a global is less coding…
(lazy as I am…)
What if I reference the element in a plugin? I have a function where I pass as variable an element (elements.xxxxx).
To know that whatever I’m doing in the plugin has allready been done for this specific element, I’ve got a hashTable holding the reference (together with some other info)
When the form gets uncached, the questions form the first post remain…
If this is not the right way of doing it, any suggestions for another approach?
Paul
no you can’t keep track of the element. (or in the onLoad of that specific form you have to set the element back in youre plugin)
But if you hold a reference to an element then when a form is unloaded
you will have an element reference that doesn’t do anything anymore.
And you could cause a memory leak (because that element could hold reference to the form itself.)
what you could do is keep a weak reference to that element in youre plugin
Then when the form is released by us. The elment will also be dereferenced in youre plugin by the jvm
Tnx Johan, will look into that.
Paul
Johan,
Been experimenting with WeakReferences and WeakHashMaps and discovered something odd.
The entries in the WeakHashMap aren’t cleared when the form/formelements should be uncached.
Bringing is down to the bare essentials, in the onLoad method of forms, I put the form object (forms[‘xxxx’]) into a WeakHaspMap as key, with just an empty string as value. Now I noticed the following:
- When you switch from formView to DesignMode an back, none of the forms are cleared!
![Shocked :shock:]()
- When you close the solution (by Close or Open New solution) and open it again, then all forms are cleared from the WeakHashMap. Needless to say, in a Client, they are also cleared, since you cannot switch to DesignMode there
![Smile :-)]()
To see if there’s a specific situation where this happens, I made a little sample solution with just one form. There the WeakHaspMap is cleared.. ![Confused :?]()
But in the solution I’m working on right now, with, offcourse, multiple forms, using JSplitPane in combination with TabPanels heavily and on most forms at least one (JToolBar) bean, the forms are consistently not cleared when switching between Form- en Design view.
Any thoughts?
Paul
then somehow they are strongly referenced.
This can be profiled with a memory profiler like yourkit (www.yourkit.com)
I can do that if you send me over all the sample things i need.
I checked all kind of things with a profiler allready, no luck. That’s why I went back to bare basics.
Will prepare you a sample solution.
Tnx,
Paul
Johan,
I think I have found the source of the problem while making a sample solution:
I’ve got quite a few forms with JToolBar beans on them. In the form’s onLoad method, I add buttons to the JToolBar. These buttons are the regular Servoy Buttons.
If I disable this adding of buttons, the forms are removed from the WeakHashMap.
See the Add-code below:
elements.JTB_footer.add(elements.edit,'Edit', 0)
elements.JTB_footer.add(elements.terminate,'Terminate', 0)
elements.JTB_footer.add(elements.remove,'Remove', 0)
elements.JTB_footer.add(elements.add,'Add', 0);
elements.JTB_footer = the JToolBar, the other elements.xxxx are buttons.
Is this technique supported?
Paul
A partial answer by myself:
The technique is supported properly in Clients (since there the WeakHashMap is cleared), but in Developer, while switching to designmode and back, something goes wrong, I guess, and the forms are not uncached.
Paul