Hi,


I use lua scripts to control hardware for experiments and use a single 
lua_State to execute different lua IUP scripts so that state information can be 
retained between scripts. I've encountered a problem that can be replicated 
using the attached files.  The main.cpp file simply creates a lua_State and 
executes a single script five times in a row. The lua script pops up an 
iup.filedlg, prints the chosen file name to stdout, and then shows a simple 
dialog with only one button that ends the script.  The first execution of the 
scripts works fine, but on the second execution the program crashes. If the 
iup.filedlg is moved into the button callback then the script executes five 
times without any problems.



Am I doing something wrong cleaning IUP between running the scripts?



Thanks for your help,



Brian
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

extern "C" {
        #include "iup.h"
        #include "iupcontrols.h"
        #include "im.h"
        #include "cd.h"
        #include "lua.h"
        #include "lauxlib.h"
        #include "lualib.h"
        #include "iuplua.h"
        #include "iupluacontrols.h"
        #include "iuplua_plot.h"
        #include "imlua.h"
        #include "cdlua.h"
        #include "cdluaiup.h"
        #include "cdluaim.h"
}

std::string LoadFile (const std::string& filename)
{
    std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary | 
std::ios::ate);
    if (ifs.fail()) {
        return std::string();
    }
    std::ifstream::pos_type fileSize = ifs.tellg();
    if (fileSize < 0) {
        return std::string();
    }

    ifs.seekg(0, std::ios::beg);

    std::vector<char> bytes(fileSize);
    ifs.read(&bytes[0], fileSize);

    return std::string(&bytes[0], fileSize);
}

int main (int argc, char* argv[])
{
        lua_State* L = luaL_newstate();                         // creates a 
new lua state
        luaL_openlibs(L);                               // Load Lua libraries

        imlua_open(L);                  // Load IM libraries
        cdlua_open (L);                 // Load CD libraries
        cdluaim_open (L);               // Load joint IM&CD libraries
        cdluaiup_open (L);

        if (argc < 2) {
                return 1;
        }

        std::string scriptname(argv[1]);
        // load script
        std::string script = LoadFile(scriptname);

        for (int i =0; i < 5; ++i) {
        iuplua_open (L);                // Load IUP libraries
        iupcontrolslua_open(L);         // Load IUP controls library
        iup_plotlua_open (L);            // Load IUP plot library

                int rstate = luaL_loadbuffer (L, script.c_str(), script.size(), 
scriptname.c_str());
                if (rstate != 0) {
                        // error
                        const char* message = lua_tostring (L, -1);
                        std::cout << message;
                        return 1;
                }

                rstate = lua_pcall (L, 0, 0, 0);
                if (rstate != 0) {
                        // error
                        const char* message = lua_tostring (L, -1);
                        std::cout << message;
                        return 1;
                }

                // force collection of garbage
                lua_gc (L, LUA_GCCOLLECT, 0);

        iuplua_close (L);
        }

        imlua_close (L);
        cdlua_close (L);
        lua_close (L);

        return 0;
}

Attachment: test2.lua
Description: test2.lua

_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to