Author Topic: variable problem when using _exit_cleanup_stuff()  (Read 4293 times)

donmadden

  • Community Member
  • Posts: 6
  • Hero Points: 0
variable problem when using _exit_cleanup_stuff()
« on: August 01, 2007, 01:32:58 pm »
Hi,

I want to use a simillar function like "_exit_cleanup_stuff()" to reset global static variables to a known reset state when the user exits SlickEdit. I have encountered a problem that when "_exit_cleanup_stuff()" sets the variable to a given value, it is not set to the expected value when the user restarts SlickEdit.

Please see an example below called "VariableWithProblem". In summary, the variable is initialised to 0, set to 100 and then set to 200 on exiting the editior. When I restart the editor, I would have expected to see the value 200 which was the last value set by '_exit_cleanup_stuff' . Instead I see the value 100? I have experimented with the obvious things like non-static, local etc but more in hope?

Does anyone have an idea why this is?

Thanks,
Don

VARIABLE WITH PROBLEM EXAMPLE
========================
#include "slick.sh"
static int  iVariableShowingProblem=0;

void setVariableToShowProblem();

//initially this will be 0 when macro is loaded
//when SlickEdit restarts, I would expect 200 or 0 but not 100?
definit()
{
   if(iVariableShowingProblem == 0)
    _message_box("definit::setVariableToShowProblem::iVariableShowingProblem is initialised to 0\n");
   else if(iVariableShowingProblem == 100)
      _message_box("definit::setVariableToShowProblem::iVariableShowingProblem is 100\n");
   else if(iVariableShowingProblem == 200)
    _message_box("definit::setVariableToShowProblem::iVariableShowingProblem is 200\n");
   
   setVariableToShowProblem();
}

//this will go from 0 to 100?
void setVariableToShowProblem()
{
   iVariableShowingProblem=100;
   if(iVariableShowingProblem == 100)
   _message_box("setVariableToShowProblem::iVariableShowingProblem is set to 100\n");

   return;
}

//set variable to 200 when user exits from editor
_exit_cleanup_stuff()
{   
   iVariableShowingProblem=200;
   if(iVariableShowingProblem==200)
     _message_box("_exit_cleanup_stuff::iVariableShowingProblem is set to 200\n");
}

Rodney

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 750
  • Hero Points: 47
Re: variable problem when using _exit_cleanup_stuff()
« Reply #1 on: August 03, 2007, 09:22:34 pm »
I suspect you are just trying to persist some data across sessions. Persisting static variables is not the way to do it. Using static variables in this way is bad practice, since the fact that static variables get stored in the state file is more of an oversight than by design. Who knows, that behavior might change in future.

I will still answer your question though  ;)

Data is only stored in the state file when it knows there is data to save. You inform SlickEdit that their is data to save in the state file by calling _config_modify_flags() after doing whatever it is that needs to be saved in the state file. You use the appropriate CFGMODIFY_ flag passed to _config_modify_flags() to tell SlickEdit what type of data needs to be saved. There is no constant for static variable data (another tip-off that persisting static variables is not a good idea). When you want to persist data across sessions using the state file, use a global variable (i.e. no 'static' specifier) and _config_modify_flags(CFGMODIFY_DEFVAR):

// The very first time your .e macro module loads, this will get initialized and NEVER AGAIN
int def_my_variable = 0;

definit()
{
   // Do something with def_my_variable
}

void set_my_variable()
{
   def_my_variable = 400;
   // Inform SlickEdit that def_my_variable needs to be saved in the state file
   _config_modify_flags(CFGMODIFY_DEFVAR);
}

Good luck.

--rodney

donmadden

  • Community Member
  • Posts: 6
  • Hero Points: 0
Re: variable problem when using _exit_cleanup_stuff()
« Reply #2 on: August 07, 2007, 01:37:28 pm »
Hi Rodney,

Thanks a million for showing us how to persist variables across sessios. I have looked at it a different way of doing it since especially as persisting data across sessions seems to be bad practise.

Thanks again for your detailed response and solutions.

Cheers,
Don

Rodney

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 750
  • Hero Points: 47
Re: variable problem when using _exit_cleanup_stuff()
« Reply #3 on: August 07, 2007, 01:49:15 pm »
Hi Don,

Persisting static data across sessions is a bad idea. Persisting global data across sessions is fine (we do it all the time).

If you (or anybody else reading this thread) have not read it yet, read "Slick-C Macro Conventions and Best Practices for End Users" at:

http://www.slickedit.com/images/stories/product_documentation/SlickC/slickcmacroconventionsandbestpracticesforendusers.pdf.

It hits on a lot of these types of topics.

Happy (Slick-C) coding!

--rodney

donmadden

  • Community Member
  • Posts: 6
  • Hero Points: 0
Re: variable problem when using _exit_cleanup_stuff()
« Reply #4 on: August 07, 2007, 08:46:37 pm »
Thanks very much Rodney for your good answers to both questions  :) It has certainly helped me ramp up on the Slick-C coding

Cheers,
Don