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

Hi,

A straight forward error is:

void lsmain(lua_State* L, SDL_Event event){ //Takes a SDL_Event
}
//and from main you are passing:
lsmain(L, &Event); //address of SDL_Event, this would definitely cause crash and i am kind a surprise why its not giving an compiling error!

Now about the thread memory allocation, you dont need to allocate any memory for the thread as all the thread libraries take care of memory allocation.

If you want to develop any C/C++ project, first thing you need to read about memory allocation.
It will help you to avoid countless hours of debug!!!

ehh i meant to drop the & because SDL_Event Event used to be SDL_Event * Event.
so if its not the thread allocation then what could it be valgrind tells me its in the lsmain() function but I'm not using any static types that I am aware of?

I know that its a Double Free or a corrupted error because without export MALLOC_CHECK=0 it spits a core dump at me

where can i find information about memory allocation then since apparently even DW seems unwilling to help me solve this "ridiculously easy bug/error" :( . I've already asked on #lua irc.freenode.net they pretty much gave me the same help you posted.

just for the record :

A) I will never post homework I am 18 and can't afford college I am strictly a hobbyist nothing more.
B) you can't possibly think this a uni assignment.

Hi,

Only way I could help now by giving some suggestions. Seeing your code I have feeling that you need to read some basic C++: reference, pointer, memory allocation.

First read a programming book/site to make sure you understand the basic memory handling using c++,
Here is a good site to start with:
http://www.learncpp.com/

I haven't used Lula or Sdl, but most probably your problem is not regarding the library itself otherwise those guys could give you proper solution, your problem is more likely how you are using the objects!!

Write some basic C++ code first, like write a vector/list/string class. You might know other language, but writing a simple code on any new language would help you to understand the basics.

Best of luck.

Nevermind I figured it out man am I such a godforsaken Moron.

upon hitting the event SDL_EVENT not only was I already closing there I was also closing in MAIN too!!!!!!!!!!!!!!!!!!!!!!!

(for everyone not familiar with C/C++)
basically in laymans terms i was calling free() twice probably triple before I found out.

lmain.h

if(event.type == SDL_QUIT){
    quit == true;
    SDL_KillThread(thread);
    SDL_Quit();
    lua_close(L);
}

lsmain.cpp

lsmain(L,Event);

lua_close(L);
SDL_Quit();

just wow man no wonder I wasn't getting any help that was such an idiotic thing of me to do.

Also C/C++ are my main languages Lua I know almost as well as C/C++ and I have been coding off and on for about 3 years I just lack common sense and a part of my brain :p

Now that I have solved my issue can a mod please delete "lua and SDL having a hard time" because that only covered one half of my problems.


Basically this is what happened during this project:

Lua and SDL both running at the same time just fine(no multithreading yet)

realized that SDL can't parse events because cin.getline was blocking the input to it(thank you

#lua from irc.freenode.net)

so tried multithreading it seemed to work with boost::thread alright wait a sec nope.

Switched to SDL_Thread worked perfectly.

then I got the glibc core dump problem was initially going to solve it with an export to

malloc_check = 0 but then realized that it shouldn't be needed in the first place.

then i came back to DW(as i always do when i need help) wait and worked for about a total of 3

hours decided to try valgrind as was recommended here(i think) and on (#lua) instead of starting

from the last error from valgrind i went up as far as i could then worked down after recompiling.

Eventually realized that I was calling lua_close(L) and SDL_Quit() multiple times. Removed and

WORKS PERFECTLY.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.