*Bug #13759 fixed - atoms modules do not start, randomly 94/18794/3
Simon Marchetto [Thu, 15 Dec 2016 15:45:39 +0000 (16:45 +0100)]
Change-Id: Iffb8c092372d8bd889086e58ffffb16d686e6a71

scilab/CHANGES.md
scilab/modules/ast/src/cpp/system_env/sci_tmpdir.cpp
scilab/modules/core/src/cpp/InitScilab.cpp

index f1c182c..21b3c13 100644 (file)
@@ -376,6 +376,7 @@ Bug Fixes
 * [#13620](http://bugzilla.scilab.org/show_bug.cgi?id=13620): `dos` called with a vector as input crashed.
 * [#13651](http://bugzilla.scilab.org/show_bug.cgi?id=13651): It was not possible to `copy` an axes into an uicontrol frame.
 * [#13757](http://bugzilla.scilab.org/show_bug.cgi?id=13757): The toolbox menu dit not load properly ATOMS modules not autoloaded.
+* [#13759](http://bugzilla.scilab.org/show_bug.cgi?id=13759): Atoms modules do not start, randomly.
 * [#13856](http://bugzilla.scilab.org/show_bug.cgi?id=13856): `messagebox` crashed under Windows in 5.5 Scilab version and updated in version 6.
 * [#13877](http://bugzilla.scilab.org/show_bug.cgi?id=13877): `<` characters included in `<screen>` areas were not rendered in the help browser.
 * [#13878](http://bugzilla.scilab.org/show_bug.cgi?id=13878): tokens([]) returns [].
index 0ca2ff0..43027ec 100644 (file)
 *
 */
 
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#ifdef _MSC_VER
+#include <process.h>
+#else
+#include <sys/types.h> /* getpid */
+#include <unistd.h> /* getpid, readlink & mkdtemp */
+#endif
+
+
 #include "configvariable.hxx"
 #include "string.hxx"
 #include "context.hxx"
 extern "C"
 {
 #include "sci_malloc.h"
-#ifdef _MSC_VER
-#include <process.h>
-#else
-#include <sys/types.h> /* getpid */
-#include <unistd.h> /* getpid, readlink & mkdtemp */
-#endif
 #include "sci_tmpdir.h"
 #include "os_string.h"
 #include "charEncoding.h"
@@ -34,7 +39,6 @@ extern "C"
 #include "setenvc.h"
 #include "getenvc.h"
 #include "localization.h"
-#include <errno.h>
 #include "removedir.h"
 #include "setenvvar.h"
 #include "getshortpathname.h"
@@ -151,31 +155,19 @@ char* computeTMPDIR()
         wchar_t wctmp_dir[PATH_MAX + FILENAME_MAX + 1];
         static wchar_t bufenv[PATH_MAX + 16];
         char *TmpDir = NULL;
-        os_swprintf(wctmp_dir, PATH_MAX + FILENAME_MAX + 1, L"%lsSCI_TMP_%d_", wcTmpDirDefault, _getpid());
+        srand((unsigned)time(NULL));
+        DWORD attribs;
+        do {
+            os_swprintf(wctmp_dir, PATH_MAX + FILENAME_MAX + 1, L"%lsSCI_TMP_%d_%d", wcTmpDirDefault, _getpid(), rand());
+            attribs = GetFileAttributesW(wctmp_dir);
+        } while ((attribs != INVALID_FILE_ATTRIBUTES) && (attribs & FILE_ATTRIBUTE_DIRECTORY));
+
         if (CreateDirectoryW(wctmp_dir, NULL) == FALSE)
         {
-            DWORD attribs = GetFileAttributesW(wctmp_dir);
-            if (attribs & FILE_ATTRIBUTE_DIRECTORY)
-            {
-                /* Repertoire existant */
-            }
-            else
-            {
-#ifdef _DEBUG
-                {
-                    char MsgErr[1024];
-                    wsprintfA(MsgErr, _("Impossible to create : %s"), wctmp_dir);
-                    MessageBoxA(NULL, MsgErr, _("Error"), MB_ICONERROR);
-                    exit(1);
-                }
-#else
-                {
-                    GetTempPathW(PATH_MAX, wcTmpDirDefault);
-                    wcscpy(wctmp_dir, wcTmpDirDefault);
-                    wctmp_dir[wcslen(wctmp_dir) - 1] = '\0'; /* Remove last \ */
-                }
-#endif
-            }
+            char MsgErr[1024];
+            wsprintfA(MsgErr, _("Impossible to create : %s"), wctmp_dir);
+            MessageBoxA(NULL, MsgErr, _("Error"), MB_ICONERROR);
+            exit(1);
         }
 
         os_swprintf(bufenv, PATH_MAX + 16, L"TMPDIR=%ls", wctmp_dir);
index f5c9271..cad16c2 100644 (file)
@@ -289,6 +289,21 @@ int StartScilabEngine(ScilabEngineInfo* _pSEI)
 
     //variables are needed by loadModules but must be in SCOPE_CONSOLE under protection
     //remove (W)SCI/SCIHOME/HOME/TMPDIR
+    types::InternalType* sci = symbol::Context::getInstance()->get(symbol::Symbol(L"SCI"));
+    types::InternalType* wsci = symbol::Context::getInstance()->get(symbol::Symbol(L"WSCI"));
+    types::InternalType* scihome = symbol::Context::getInstance()->get(symbol::Symbol(L"SCIHOME"));
+    types::InternalType* home = symbol::Context::getInstance()->get(symbol::Symbol(L"home"));
+    types::InternalType* tmpdir = symbol::Context::getInstance()->get(symbol::Symbol(L"TMPDIR"));
+
+    sci->IncreaseRef();
+    if (wsci)
+    {
+        wsci->IncreaseRef();
+    }
+    scihome->IncreaseRef();
+    home->IncreaseRef();
+    tmpdir->IncreaseRef();
+
     symbol::Context::getInstance()->remove(symbol::Symbol(L"SCI"));
     symbol::Context::getInstance()->remove(symbol::Symbol(L"WSCI"));
     symbol::Context::getInstance()->remove(symbol::Symbol(L"SCIHOME"));
@@ -298,8 +313,27 @@ int StartScilabEngine(ScilabEngineInfo* _pSEI)
     //open a scope for macros
     symbol::Context::getInstance()->scope_begin();
 
+    //put var in good scope
     Add_All_Variables();
-    SetScilabVariables();
+    symbol::Context::getInstance()->put(symbol::Symbol(L"SCI"), sci);
+    if (wsci)
+    {
+        symbol::Context::getInstance()->put(symbol::Symbol(L"WSCI"), wsci);
+    }
+
+    symbol::Context::getInstance()->put(symbol::Symbol(L"SCIHOME"), scihome);
+    symbol::Context::getInstance()->put(symbol::Symbol(L"home"), home);
+    symbol::Context::getInstance()->put(symbol::Symbol(L"TMPDIR"), tmpdir);
+
+    sci->DecreaseRef();
+    if (wsci)
+    {
+        wsci->DecreaseRef();
+    }
+    scihome->DecreaseRef();
+    home->DecreaseRef();
+    tmpdir->DecreaseRef();
+
 
     symbol::Context::getInstance()->protect();
     //execute scilab.start