Clear all variables?

Is there an easy way to clear all global variables from memory, or close an app and restart it automatically to accomplish this in Xojo?

This is an odd question. What are you seeing that you think that clearing global variables from memory will solve?

You can restart the app. Though I believe you will need to create a ‘helper’ app that runs in the background. When you say ‘global’ variables what do you mean? Are your variables in modules or in the ‘App’ object or both. The best way to do this would probably be to set each variable to null (depending on the type (for example have objects assigned to null, strings assigned to “” and numbers assigned to 0 (or -1 if you find that works for your app).

You could create a class that stores all of these variables and then reinstitute it when you want to reset the values.

I can’t help but wonder if you don’t mean to reinitialize global variables to zero (int = 0, str = “”, etc) rather than clearing them from memory. Clearing them from memory would, seemingly, be quite counterproductive.

Well, I also suggested creating a class with all of the ‘global’ variables stored in it and then reinstituted it when the values need clearing. That should clear it from memory AFAIK.

Just like Bob, I am wondering what you are trying to accomplish. But here is how you do it in Mac OS. The same can be done in Windows if needed.

  • In your app create a boolean property in App called relaunch
  • In the App close event, put this :

Sub Close() if relaunch = true then dim f as folderitem = getfolderitem("shell.sh") dim s as new shell s.mode = 0 s.execute(f.shellpath) end if End Sub

shell.sh is a text file containing only one line :

open myapp.app

In terminal, give the execute permission for shell.sh :

chmod 755 shell.sh

Copy shell.sh to the same folder as myapp.app

When you quit, if relaunch is true, the app relaunches.

This works only for a built app, because when you quit the debug app, it is deleted, so shell.sh has nothing to launch.

[quote=121976:@Michel Bujardet]Just like Bob, I am wondering what you are trying to accomplish. But here is how you do it in Mac OS. The same can be done in Windows if needed.

  • In your app create a boolean property in App called relaunch
  • In the App close event, put this :

Sub Close() if relaunch = true then dim f as folderitem = getfolderitem("shell.sh") dim s as new shell s.mode = 0 s.execute(f.shellpath) end if End Sub

shell.sh is a text file containing only one line :

open myapp.app

In terminal, give the execute permission for shell.sh :

chmod 755 shell.sh

Copy shell.sh to the same folder as myapp.app

When you quit, if relaunch is true, the app relaunches.

This works only for a built app, because when you quit the debug app, it is deleted, so shell.sh has nothing to launch.[/quote]
Does this work on Windows, Mac and Linux?

In Linux I believe the very same manipulation will work, except the name of the executable will differ (Mac OS X underlining Unix and Linux mostly share the same shell script language).

In Windows instead of shell.sh you want to name your relauncher shell.bat and place into it :

start myapp.exe

See http://ss64.com/nt/start.html

No need for chmod, Windows does not use permissions.

[quote=121984:@Michel Bujardet]In Linux I believe the very same manipulation will work, except the name of the executable will differ (Mac OS X underlining Unix and Linux mostly share the same shell script language).

In Windows instead of shell.sh you want to name your relauncher shell.bat and place into it :

start myapp.exe

See http://ss64.com/nt/start.html

No need for chmod, Windows does not use permissions.[/quote]
Okay thanks. Just making sure for others who may visit the thread and if I want to do this in the future also.
Thanks

Michel: I tried your method, placing the lines in the Close part of the app, and the code with “shell.bat” in the folder with that one line in it, and the Boolean, and I get all kinds of errors upon running the compiled app. Using Windows 8.1. Like this one first:

Yes, this is what I mean…a quick easy way to reset them all.

Is this somethig you are used to do in some other programming environment? I use Matlab and I know “clear all” is there for you, maybe also in other environments?

Since we don’t know what you are doing this is just a guess, but wouldn’t using the right scope for each variable and classes be a solution to what you are trying to do?

Julen

[quote=122007:@Derek DiBenedetto]Michel: I tried your method, placing the lines in the Close part of the app, and the code with “shell.bat” in the folder with that one line in it, and the Boolean, and I get all kinds of errors upon running the compiled app. Using Windows 8.1. Like this one first:
[/quote]

I do not see how my code could trigger a plugin error. Here is exactly what to do :

  • In App, add a property called relaunch as boolean
  • in App, add the Close event, with this inside :
  if relaunch = true then
    dim f as folderitem = getfolderitem("shell.bat")
    dim s as new shell
    s.mode = 0
    s.execute(f.shellpath)
  end if
  • Create a text file with Notepad called shell.bat with this inside :
start myapp.exe

where myapp.exe should be the actual name of your program

  • Build your app
  • In the folder of the build that opens, place shell.bat

You can place the following code in a button Action event to trigger a relaunch :

App.relaunch = True quit

I tested that with Windows 8.1 before posting, so I am positive it works. If for some reason your app still got errors, try with a new project.

Apart from a relaunch, you could as well put in a Reset method all your variables like :

a = 0 text = "" Window1.yada = 0 Window1.msg = "" //and so on

You list only windows properties and global variables, as event and methods variables are cleared upon exit.

Of course, if you have zilions of properties, it may take a while to write, but afterward, every time you create a new property, all you have to do is add it to Reset.

You can end the Reset method with a show that presents the user with the default window.

I believe what the OP is looking for is a way to do a “clean restart” (the same as if you quit and restarted)… in which case it may not be a need to CLEAR all the variables, but to reinit them to specific values (which may be default values or may be specific values)…

But I can also see in some cases the number of variables to be cumbersome to create a app wide “reinit” method.

In older versions of BASIC… you could issue the RUN command internally, which would reset and restart everything

Using a helper (bat, applescript, shell etc) could be made to work, but would most likely invalidate the application from being accepted by either the Apple or Microsoft stores.

Have you considered creating a class to hold your variables. The instance of the class could be in a global module. You could then “reset” the contents of the object by overwriting it with a new instance.

Messages like that are what you see when a compiled app is running from a location that lacks the runtime DLL support files.

Waynes’ answer seems best to me.
A global class, holding all the variables.

Call it GLclass
create one instance when the app starts with
GL = new GLclass

Access the variables as GL.fontname GL.WindowLastOpened and whatever
To reset things back to ground zero
GL = new GLClass

That is what I did above with a shell. It works well on Mac and Windows, but indeed, it may be frowned upon by the Apple Store reviewers. From what I know of the onboarding program for Xojo applications in the Windows Store, there is no manual review, and the only tests are automated, so chances are the shell manipulation would never be a problem.

For Metro it is simply not possible : new API applications have no access to shell, and even if they did, applications cannot quit. The Metro interface is such that a program never quits, it is up to the system to kill it eventually. Just like in a phone.

The concept seems elegant, but in fact variables would then all have to be global. So much for scope. I wonder if introspection would not allow to scan all properties in the app and reset all strings to “” and numeric values to zero. in the reset method I suggested above, I also left out control properties. A reset analogous to the old Basic RUN command would have to erase the TextAreas and TextFields Text properties, put back the sliders to zero, maybe uncheck checkboxes, and remove rows from Listboxes. Among others. Then the method would have to start over at the default page, and eventually load settings again.

It is quite possible, but we are far from the OP wish of “an easy way”.

They are now. That’s the core of the problem. Of course, they probably shouldn’t be. That would be a better solution in the long run.

Resetting variables is one thing. To start enew, the app also needs to reset controls properties, such as TextField.text. This kind of code could be used :

Dim lastOffset As Integer = WindowCount - 1 For i As Integer = 0 To lastOffset for ii as integer = 0 to Window(i).ControlCount-1 if Window(i).control(ii) isa TextField then TextField(Window(i).control(ii)).Text = "" if Window(i).control(ii) isa TextArea then TextArea(Window(i).control(ii)).Text = "" next Next

Of course, all controls must be reset, I just put TextAreas and TextFields.