Memory allocation problems
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
Related Article: Memory leaks
is a C++ discussion thread by Qusto that has 3 replies and was last updated 11 months ago.
Celtrix
Junior Poster in Training
67 posts since Jan 2011
Reputation Points: 8
Solved Threads: 4
Skill Endorsements: 0
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
Celtrix
Junior Poster in Training
67 posts since Jan 2011
Reputation Points: 8
Solved Threads: 4
Skill Endorsements: 0
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.
Celtrix
Junior Poster in Training
67 posts since Jan 2011
Reputation Points: 8
Solved Threads: 4
Skill Endorsements: 0
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.
Celtrix
Junior Poster in Training
67 posts since Jan 2011
Reputation Points: 8
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
alwaysLearning0