Proper way to close Web Containers

I will often have multiple web containers open in a page, and need to close them out and open new ones. When doing this, I’ll usually use something along the lines of:

[code]for i as integer = MyProductContainers.Ubound DownTo 0
MyProductContainers(i).Close
MyProductContainers(i) = nil
next

redim MyProductContainers(-1)[/code]

However, often times the containers will still appear in the view or not fully close and remove themselves from the page. Also I’m getting a large number of javascript errors (undefined controls when evaluating control.hasFinishedLoading, etc) related to the code.

Is setting it to nil causing the problem? Is there a better way of safely removing the controls to ensure they close visually on the page and also don’t eat up memory after they are closed?

In principle, you don’t need to set to nil after you close.

[quote=477691:@Tom Iwaniec]I will often have multiple web containers open in a page, and need to close them out and open new ones. When doing this, I’ll usually use something along the lines of:

[code]for i as integer = MyProductContainers.Ubound DownTo 0
MyProductContainers(i).Close
MyProductContainers(i) = nil
next

redim MyProductContainers(-1)[/code]

However, often times the containers will still appear in the view or not fully close and remove themselves from the page. Also I’m getting a large number of javascript errors (undefined controls when evaluating control.hasFinishedLoading, etc) related to the code.

Is setting it to nil causing the problem? Is there a better way of safely removing the controls to ensure they close visually on the page and also don’t eat up memory after they are closed?[/quote]
So… you might be hurting yourself here. When you call Close, code is queued to be sent to the browser to close the control on the browser. I guess it’s possible that you’re interrupting that process, but I don’t think I’ve heard of this before.

Thanks Greg. So should I just not set to nil and hope it deals with the rest itself? Getting a lot of javascript errors on things that work fine most of the time (and in debugging) but then crash on a live server with the aforementioned issues.

In my experience, JavaScript errors are from code that tries to access controls too early (doing things in the open event that need to be done in the shown event), from trying to access a control after it has been closed or code that is being incorrectly added, whether by ExecuteJavascript or a WebSDK control that hasn’t been tested enough.

The specific errors are important however and I’d be interested to see one or more to see if we are on the right track.

It’s also important to remember that for each control that you put on a layout, there are actually three instances. One in your Xojo code and two in the browser. The browser has one that is in the DOM which is the visual presentation and accepts events and one that is instantiated in the Xojo JavaScript framework to handle the browser-side logic. If any of these are missing when the frameworks try to access them, you may get an error.

@Greg O’Lone , you didn’t answer the initial question. :slight_smile: What is the best way to remove a web container from the browser/memory once it is open. I’ve always assumed it was using ‘.close’. Is this correct, or am I causing more traffic/memory load than I should be.

Thanks…

I open and close containers in webpages too. The difference in my code is that I do not assign to nil and I use a weakref to store the container reference in my array.
And do not redim after the FOR, do it later in the next pass clealing the weakref pointing to nil.

[quote=477776:@Greg O’Lone]In my experience, JavaScript errors are from code that tries to access controls too early (doing things in the open event that need to be done in the shown event), from trying to access a control after it has been closed or code that is being incorrectly added, whether by ExecuteJavascript or a WebSDK control that hasn’t been tested enough.

The specific errors are important however and I’d be interested to see one or more to see if we are on the right track.[/quote]

I can confirm this.
In my apps setting to nil a webcontainer variable after calling close() never caused a problem.

It looks like the issue is happening because on my end in the app everything thinks that it loaded correctly, but somewhere along the process, the control doesn’t fully load. So when I go to close the control the javascript error appears.

I can reload the same page and visibly see it failing on a DIFFERENT container each time, even though there is no change in my code between reloads. Each container has a WebImageView loading an image from a url. I’m guessing it has something to do with this because they load sequentially, but if one doesn’t load properly, all WebImageViews in containers after that container also do not load.

Could this be some sort of server/backend issue with loading too many WebImageViews? There are about 30 on my example page.

[quote=477804:@Robert Litchfield]@Greg O’Lone , you didn’t answer the initial question. :slight_smile: What is the best way to remove a web container from the browser/memory once it is open. I’ve always assumed it was using ‘.close’. Is this correct, or am I causing more traffic/memory load than I should be.

Thanks…[/quote]
The correct way for s to simply call Close.

Excellent…thank you @Greg O’Lone!