I'm not known to be a very memory friendly guy but after about 4 hours debugging my lua+sdl in C++ I finally figured out how to use threads but now I have another problem. I apparently have zero knowledge of memory manangement but thats kind of ok so I've narrowed the error down to the function void lsmain()
lsmain.h
#ifndef _LSMAIN_H_
#define _LSMAIN_H_
#include <lua.hpp>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
using namespace std;
bool quit = false;
void interpret(lua_State* L){
int error;
int x = 0;
char buffer[256];
while(x == 0){
cout<<"Lua >> ";
cin.getline(buffer,256);
error = luaL_loadbuffer(L,buffer,strlen(buffer), "line") || lua_pcall(L,0,0,0);
if(error){
cout << lua_tostring(L,-1) << endl;
lua_pop(L,1);
}
}
}
int handler(void* data){
while(quit == false){
interpret((lua_State*) data);
}
return 0;
}
void lsmain(lua_State* L, SDL_Event event){
SDL_Thread * thread ;
cout<<"Entering lsmain...\n";
thread = SDL_CreateThread(handler,L);
for(;;){
SDL_PollEvent(&event);
if(event.type == SDL_QUIT){
quit = true;
lua_close(L);
SDL_Quit();
}
}
}
#endif
Should I call malloc for thread?
lsmain.cpp
#include "lsmain.h"
int main(int argc, char ** argv){
SDL_Surface * screen = NULL;
SDL_Event Event;
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return -1;
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE);
if(screen == NULL){
cout<<"Screen Failed...\n";
exit(1);
}
SDL_WM_SetCaption("SDL + Lua + C++",NULL);
lua_State * L;
L = lua_open();
luaL_openlibs(L);
lsmain(L, &Event);
return 0;
}
NOTE: this compiles fine but upon pushing the X button to exit it dumps the core