lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



----- Original Message ----- From: "Mike Pall" <mikelu-0602@mike.de>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: 25 February 2006 22:30
Subject: Re: Running BoundsChecker on 5.0.2


Now, most of these problems might just be BoundsChecker's own fault, but
there are other, potentially more worrisome reports: a few dangling pointer
errors ("Pointer 0x..., allocated by realloc, has already been freed")
which might actually be real problems.

These should be investigated. But I guess you have to reduce the
number of false alarms first. Maybe you need a different tool or
just a newer version.

It turns out the dangling pointer errors are generated by this code, which loads luasocket right after having created and initialized the lua_State:

   // Define function to load a package from a string.
   acName = "_loadpackage";
   const char* acCmd =
       "function _loadpackage(name,text)\n"
       "   local f, e = loadstring(text)\n"
       "   if (not f) then\n"
       "       error(e)\n"
       "       return\n"
       "   end\n"
       "   local m = {}\n"
       "   setmetatable(m, {__index = _G})\n"
       "   setfenv(f, m)\n"
       "   f()\n"
       "   _LOADED[name] = m\n"
       "end\n";
   iResult = lua_dobuffer(m_pkVM,acCmd,strlen(acCmd),acName);
   if ( iResult )
   {
       Application::DisplayMessage(string("Can't define ") + acName);
       DestroyVM();
       return;
   }
   lua_pushstring(m_pkVM,acName);
   lua_gettable(m_pkVM,LUA_GLOBALSINDEX);
   int f = lua_gettop(m_pkVM);

   // Load all internal Lua packages.
   const int iNumPackages = 8;
   const char* aacPackages[iNumPackages] =
   {
       acSocketDotLua,
       acLTN12DotLua,
       acMimeDotLua,
       acTPDotLua,
       acURLDotLua,
       acFTPDotLua,
       acHTTPDotLua,
       acSMTPDotLua
   };
   const char* aacNames[iNumPackages] = { "socket", "ltn12", "mime",
       "tp", "url", "ftp", "http", "smtp" };
   for (int i = 0; i < iNumPackages; ++i)
   {
       lua_pushvalue(m_pkVM,f);
       lua_pushstring(m_pkVM,aacNames[i]);
       lua_pushstring(m_pkVM,aacPackages[i]);
       if ( lua_pcall(m_pkVM,2,0,0) )
       {
           string kMsg = "Error loading '";
           kMsg += aacNames[i];
           kMsg += "' package: ";
           kMsg += lua_tostring(m_pkVM,-1);
           Application::DisplayMessage(kMsg);
           DestroyVM();
           return;
       }
   }
   lua_pop(m_pkVM,1);

I cannot see any particular problems with this code, which I'm not particularly proud of but it's needed in the context of our ActiveX control...

Thanks,
Dario