Completely purging a stack from memory when closed....

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Completely purging a stack from memory when closed....

Post by JustinW42 » Mon Feb 23, 2015 12:23 pm

I have made a lot of Livecode programs so far, mostly tools for my business and other side projects I am exploring. There is one program in particular, however, which seems to leave residual data behind when I close the standalone application. The stack in question has many different sub-stacks (toolbars, color palettes, etc) which are sometimes loaded for various purposes, but when I close the main stack it seems to create a background process that I can't figure out how to get rid of....

If i open the program and close it immediately, no background process is created. When I open the program, run through a few of the initial buttons (loading data and whatnot, including at least one initial sub-stack), then close the program, no background process is created. It is not until I run through the initial data loading, then move on (this particular program allows you to select an uploaded image, then load it into a design lab for manipulation and to design a custom phone cover or other product), then close the app, that I suddenly see a background process appear in Windows Task Manager.

In the main stack "preopenStack" handler, I set the destroyStack property of ALL stacks / sub-stacks to true, hoping that would clear the background process. Then I implemented some code in the CloseStackRequest handler, which forcibly closes all the stacks / sub-stacks in the program (even if they are not currently open, just to be sure) - no matter what I try, the background process is still created when I close the standalone program.

I know it is probably something simple I am overlooking, but I have perused the code a dozen times and googled for hours to find some sort of help...... So far, nothing has worked. None of the other programs I have created seem to leave behind a background process after I close the app (I haven't thoroughly tested each one to be sure, but so far I have only noticed it with this one particular stack) - it must be something I am forgetting to do, since I use so many different sub-stacks for this particular program.... Any help would be greatly appreciated!
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by Klaus » Mon Feb 23, 2015 2:21 pm

Hi Justin,

hm, do you have any "pending messages" (the one that are created when using "send xyz to...in...") in your stack(s)?
That will also hinder a stack from closing.

And/or you could try to "delete stack name_of_stack", see dictionary for this special meaning of delete, to remove
your stack from memory.


Best

Klaus

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Tue Feb 24, 2015 7:35 am

I will look into that Delete command! Would I run in in the CloseStackRequest handler? I will go ahead and read up on it a bit, but while I do my homework it would give me a head start to know where the code will be placed. :)

Regarding any potential messages that could be pending - I don't think so..... There are no auto-updating messages being sent in this particular app (whereas in other stacks I have created where I send a command every XXX seconds to refresh all the visible data, I do not have the residual background process issue), but my general process looks like this (I know it is next to useless without seeing the actual code, but maybe knowing what I am doing might trigger a suggestion):

1. User clicks button to load data from a database (several tables are loaded into arrays).
2. Substack popup appears, allowing user to select which event they are at (to determine several variables about how the software runs and whatnot).
3. Once a selection is made, the substack popup is closed, then the user clicks another button on the main stack to "begin the design process".
4. A second substack popup appears, allowing the user to select an image from the server which has been previously uploaded (or upload their own image).
* NOTE: If I close the program at this point, no background process is left in the Task Manager queue!

5. Once a selection is made, the second substack popup is closed and a third is opened automatically to allow the user to select which product they want to add the image to.
6. Once a selection is made, the third substack popup is closed and the "Design Lab" is loaded, complete with a background image of the product, as well as the image the user selected at the beginning of the process.
* NOTE: If I close the program at this point, a background process will show up in the Task Manager queue

That information probably isn't very helpful, but at least we know what I am trying to accomplish. Well, technically I have already accomplished it - we have been using the standalone application for our business for many months now, with great success! However, the residual background processes can sometimes compound other small "bugs" still left in the software, creating annoying problems that should be easily resolved once I track them down. First step - figure out this stupid issue! I'm off to read about the Delete command.....
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Tue Feb 24, 2015 7:42 am

I have been experimenting with using libURLSetStatusCallback when I download or upload images via FTP (with no success, yet) - however, could this be a potential issue if it continues trying to listen for a download to finish, and I never actually end the callback listener in code? Like I said, I haven't really got a handle on how it works, so I could be setting listeners which create some sort of residual background processes?

Just a random theory. If I have time tonight, I might comment them out to see if that makes any difference.
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Tue Feb 24, 2015 9:19 am

I implemented the Delete stack command in the CloseStackRequest handler..... No dice. I also tried commending out the download listener, but that simply broke the code (apparently I am using it functionally to execute additional code after the download is complete, so that is good news - I still don't forcefully end the listener in the code, however).

There are about 13 stacks which I close / delete in the code, and interestingly there are 2 stacks which create errors when I try to Delete them - one is the main stack ("AP Mobile Website Design Tool"), and the other is a substack ("GeneratePNG") that never even gets opened during the course of the program (it is simply used to position images and take background snapshots).... I commented out the Delete command for both of these stacks in order to avoid the errors, but could that be a clue as to what is keeping the background process running?

Code: Select all

on CloseStackRequest
   answer info "Are you sure you want to close the Design Lab?" with "Yes" or "No"
   if it = "Yes" then
      ## close stack:
      close stack "BackgroundStack"
      if there is a stack "BackgroundStack" then
         delete stack "BackgroundStack"
      end if
      
      close stack "AP Mobile Website Design Tool"
      if there is a stack "AP Mobile Website Design Tool" then
         --delete stack "AP Mobile Website Design Tool"
      end if
      
      close stack "ColorPalette"
      if there is a stack "ColorPalette" then
         delete stack "ColorPalette"
      end if
      
      close stack "ImportImage"
      if there is a stack "ImportImage" then
         delete stack "ImportImage"
      end if
      
      close stack "ProgressBarSub"
      if there is a stack "ProgressBarSub" then
         delete stack "ProgressBarSub"
      end if
      
      close stack "SelectUploadedImage"
      if there is a stack "SelectUploadedImage" then
         delete stack "SelectUploadedImage"
      end if
      
      close stack "Toolbar"
      if there is a stack "Toolbar" then
         delete stack "Toolbar"
      end if
      
      close stack "ChooseEvent"
      if there is a stack "ChooseEvent" then
         delete stack "ChooseEvent"
      end if
      
      close stack "CropImage"
      if there is a stack "CropImage" then
         delete stack "CropImage"
      end if
      
      close stack "GeneratePNG"
      if there is a stack "GeneratePNG" then
         --delete stack "GeneratePNG"
      end if
      
      close stack "GenerateThumb"
      if there is a stack "GenerateThumb" then
         delete stack "GenerateThumb"
      end if
      
      close stack "LoadExistingDesign"
      if there is a stack "LoadExistingDesign" then
         delete stack "LoadExistingDesign"
      end if
      
      close stack "ThumbnailGenerator"
      if there is a stack "ThumbnailGenerator" then
         delete stack "ThumbnailGenerator"
      end if
      
      PASS closestackrequest
   end if
   ## no other action neccessary
end CloseStackRequest
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Completely purging a stack from memory when closed....

Post by Simon » Tue Feb 24, 2015 10:01 am

JustinW42,
hmmm... you haven't said yet if you've deleted the pending messages.

Code: Select all

repeat for each line aLine in the pendingMessages
cancel item 1 of aLine
end repeat
Now that will remove all pending messages.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Tue Feb 24, 2015 12:22 pm

I just added that code to the handler, immediately prior to closing the stacks. There is still a a background process being created every time I close the standalone application. :-/
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by Klaus » Tue Feb 24, 2015 1:33 pm

Hi Justin,

HINT: Instead of scripting "delete stack xyz" for all of your stacks, it might be a better idea to directly
set these properties for your stacks in the inspector!

Check "destroyStack" and "destroyWindow"*** for each of your stacks, that will have the same effect.
***In the LC preferences I choose: Property labels are Name of Livecode property

For your "background process" that is being created, we will need to look at your code however.


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Completely purging a stack from memory when closed....

Post by jacque » Tue Feb 24, 2015 6:27 pm

Another note about delete stack: never delete a substack unless you really do want it to disappear from the stack file forever. Deleting a substack removes it just like deleting a button removes it from the card. Only delete mainstacks to remove them from memory.

My guess is that there is an open process somewhere. Do you close the database? Also, if there are any pending messages you can see those in the pending messages pane of the message box, so try keeping an eye on that.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Wed Feb 25, 2015 7:38 am

Ok I am getting closer to solving this, I think...... For the record, I do close all my database connections right after I grab the data I need from the table(s) - I learned about this one the hard way, and it has really upped my Livecode game!! lol

As suggested, I monitored the message box for pending messages to see what was happening while I ran through the program. Shortly before the "point of no return" (the point where if I try to close the standalone program, a background process remains in memory) I noticed a message that referenced ulTickleMe, which was not something I programmed in so I did some googling and found this link:

http://www.runrev.com/pipermail/use-liv ... 85713.html

In the posted information, Dave Cragg explains that this message is being sent during (and for a short period after) an FTP command is executed, which would explain why I am seeing it... Shortly before this step, my users select their image from the server and I use FTP to download it to the design lab! I tried waiting a few minutes after I went through this step, then tried closing the program and BAM, it did not leave any background processes running (I got the idea after I left the development app open in LC for awhile, waiting to see if the ulTickleMe message disappeared on its own - it eventually did, so I figured something similar must be happening when I am running the standalone program).

So now I know that it is good practice to not close my program for a few minutes after running anything that requires the use of FTP, but this seems like a neanderthal way of handling the situation..... I thought the code provided by Simon might close the processes before my program closes, but it doesn't seem to forcefully resolve the issue. Is there another way I can manually end this particular bit of code, to ensure it is not still running if one of my users gets antsy and closes the program too quickly?
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Completely purging a stack from memory when closed....

Post by jacque » Wed Feb 25, 2015 9:32 am

Good sleuthing. In a shutdown handler you can do this:

Code: Select all

repeat for each line L in the pendingMessages
  cancel item 1 of L
end repeat
That will clear everything out.

Edit : oops, I see that's what Simon suggested. Should work though. What handler did you use it in?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Completely purging a stack from memory when closed....

Post by Simon » Wed Feb 25, 2015 11:40 am

Ahh... I just remembered "resetAll".
Now that is written up in the dictionary like it will kill all the system processes. Like it's dangerous (as in don't ship it).
Try it before the pendingMessages thing. Does that do it?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Wed Feb 25, 2015 1:11 pm

Simon wrote:Ahh... I just remembered "resetAll".
Now that is written up in the dictionary like it will kill all the system processes. Like it's dangerous (as in don't ship it).
Try it before the pendingMessages thing. Does that do it?

Simon
And we have a WINNER!!!! Thanks so much for the help, Simon, Klaus and Jacque! Once I implemented the resetAll into the handler, everything works as expected and no extra background processes are left lingering in the background! This will definitely help me out as I develop more complex programs. You guys rock!

For the record (and for anyone struggling with similar issues), here is what my CloseStackRequest handler now looks like:

Code: Select all

on CloseStackRequest
   answer info "Are you sure you want to close the Design Lab?" with "Yes" or "No"
   if it = "Yes" then
      ## close stack:
      
      if the openSockets is not empty then resetAll
      
      repeat for each line aLine in the pendingMessages
         cancel item 1 of aLine
      end repeat
      
      close stack "BackgroundStack"
      if there is a stack "BackgroundStack" then
         delete stack "BackgroundStack"
      end if
      
      close stack "AP Mobile Website Design Tool"
      if there is a stack "AP Mobile Website Design Tool" then
         --delete stack "AP Mobile Website Design Tool"
      end if
      
      close stack "ColorPalette"
      if there is a stack "ColorPalette" then
         delete stack "ColorPalette"
      end if
      
      close stack "ImportImage"
      if there is a stack "ImportImage" then
         delete stack "ImportImage"
      end if
      
      close stack "ProgressBarSub"
      if there is a stack "ProgressBarSub" then
         delete stack "ProgressBarSub"
      end if
      
      close stack "SelectUploadedImage"
      if there is a stack "SelectUploadedImage" then
         delete stack "SelectUploadedImage"
      end if
      
      close stack "Toolbar"
      if there is a stack "Toolbar" then
         delete stack "Toolbar"
      end if
      
      close stack "ChooseEvent"
      if there is a stack "ChooseEvent" then
         delete stack "ChooseEvent"
      end if
      
      close stack "CropImage"
      if there is a stack "CropImage" then
         delete stack "CropImage"
      end if
      
      close stack "GeneratePNG"
      if there is a stack "GeneratePNG" then
         --delete stack "GeneratePNG"
      end if
      
      close stack "GenerateThumb"
      if there is a stack "GenerateThumb" then
         delete stack "GenerateThumb"
      end if
      
      close stack "LoadExistingDesign"
      if there is a stack "LoadExistingDesign" then
         delete stack "LoadExistingDesign"
      end if
      
      close stack "ThumbnailGenerator"
      if there is a stack "ThumbnailGenerator" then
         delete stack "ThumbnailGenerator"
      end if
      
      PASS closestackrequest
   end if
   ## no other action neccessary
end CloseStackRequest
It may seem like overkill, but it does the job and I don't want to break it by reducing the code. lol. Also, I recognize that I might need to go add the CloseStackRequest handler to all the other stacks, as well, and remove the code which does all the actual "closing" work in these substacks - the last thing I need is to close a minor substack in the code and see the logical message path look for the CloseStackRequest handler, only to find it in the main stack (and subsequently closing everything, un-intentionally).... I don't know if this would be a real concern, but I guess "better safe than sorry" is a good rule of thumb for programming.
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by Klaus » Wed Feb 25, 2015 4:16 pm

Hi Justin,

it may not only SEEM like overkill, it IS overkill! :D
Anyway, as long as it works out fine, who cares.

Here a hint to save a lot of typing!

Code: Select all

...
## Create a list of all your (main) stacks:
put "BackgroundStack,AP Mobile Website Design Tool,ColorPalette,ImportImage,ProgressBarSub" into tStackList

## Then simply loop through this list:
repeat for each item tStack in tStackList
      close stack tStack
      if there is a stack tStack then
         delete stack tStack
      end if
end repeat
...
You get the picture. :D


Best

Klaus

JustinW42
Posts: 52
Joined: Sun Nov 30, 2014 7:22 am
Contact:

Re: Completely purging a stack from memory when closed....

Post by JustinW42 » Thu Feb 26, 2015 7:57 am

Thanks Klaus! That is a much better idea than trying to keep track of any and all new substacks I create (of which there are many) - I will definitely re-loop my code to take advantage of your great suggestion. Thanks again!

I am loving Livecode more and more each day, and I hope to eventually spend more time here on the forums helping other people the way I have been helped so far. That "background process" bug has been an annoying thorn in my side for a few months now (although I haven't been actively tackling it the entire time), so the fact that we were able to collectively resolve it in a matter of days once I finally sat down and crowd-sourced a solution shows how powerful the community can be!

Kudos to the masters. 8)


EDIT: I see that I would still need to keep track of the individual substack names, but using the condensed code will still be a huge benefit!
-------------------------------------------------------------------------------------------------------------
Some things I do: http://www.katanadtg.com --- http://www.dtgprintsolutions.com

Post Reply