Skip to content

Commit 94d76ac

Browse files
author
rtri
committed
fix #5483; LuaContextData drags in way too much junk
1 parent 1545bb7 commit 94d76ac

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

rts/Lua/LuaContextData.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct GLMatrixStateTracker {
4949
return matrixData.mode;
5050
}
5151

52-
int &GetDepth(unsigned int mode) {
52+
int& GetDepth(unsigned int mode) {
5353
switch (mode) {
5454
case GL_MODELVIEW: return matrixData.modelView;
5555
case GL_PROJECTION: return matrixData.projection;
@@ -63,7 +63,7 @@ struct GLMatrixStateTracker {
6363

6464
bool PushMatrix() {
6565
unsigned int mode = GetMode();
66-
int &depth = GetDepth(mode);
66+
int& depth = GetDepth(mode);
6767
if (!listMode && depth >= 255)
6868
return false;
6969
depth += 1;
@@ -72,7 +72,7 @@ struct GLMatrixStateTracker {
7272

7373
bool PopMatrix() {
7474
unsigned int mode = GetMode();
75-
int &depth = GetDepth(mode);
75+
int& depth = GetDepth(mode);
7676
if (listMode) {
7777
depth -= 1;
7878
return true;
@@ -165,6 +165,7 @@ struct GLMatrixStateTracker {
165165

166166

167167
struct luaContextData {
168+
public:
168169
luaContextData()
169170
: owner(nullptr)
170171
, luamutex(nullptr)
@@ -184,6 +185,15 @@ struct luaContextData {
184185
, selectTeam(CEventClient::NoAccessTeam)
185186
, parser(nullptr) {}
186187

188+
void Clear() {
189+
shaders.Clear();
190+
textures.Clear();
191+
fbos.Clear();
192+
rbos.Clear();
193+
displayLists.Clear();
194+
}
195+
196+
public:
187197
CLuaHandle* owner;
188198
spring::recursive_mutex* luamutex;
189199

rts/Lua/LuaDisplayLists.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@ struct SMatrixStateData {
2020

2121
class CLuaDisplayLists {
2222
public:
23-
CLuaDisplayLists()
24-
{
25-
active.push_back(DLdata(0));
26-
}
27-
23+
CLuaDisplayLists() { Clear(); }
2824
~CLuaDisplayLists()
2925
{
3026
// free the display lists
31-
for (int i = 0; i < (int)active.size(); i++) {
27+
// NOTE:
28+
// it is not an error to delete a list with id=0, but we might
29+
// be called from ~LuaParser which can run in multiple threads
30+
// and the null-list is always present (even after Clear())
31+
for (size_t i = 1; i < active.size(); i++) {
3232
glDeleteLists(active[i].id, 1);
3333
}
3434
}
3535

36-
void Clear()
37-
{
36+
void Clear() {
3837
unused.clear();
3938
active.clear();
4039
active.push_back(DLdata(0));
@@ -44,27 +43,25 @@ class CLuaDisplayLists {
4443

4544
GLuint GetDList(unsigned int index) const
4645
{
47-
if (index < active.size()) {
46+
if (index < active.size())
4847
return active[index].id;
49-
} else {
50-
return 0;
51-
}
48+
49+
return 0;
5250
}
5351

5452
SMatrixStateData GetMatrixState(unsigned int index) const
5553
{
56-
if (index < active.size()) {
54+
if (index < active.size())
5755
return active[index].matData;
58-
} else {
59-
return SMatrixStateData();
60-
}
56+
57+
return SMatrixStateData();
6158
}
6259

6360
unsigned int NewDList(GLuint dlist, SMatrixStateData& m)
6461
{
65-
if (dlist == 0) {
62+
if (dlist == 0)
6663
return 0;
67-
}
64+
6865
if (!unused.empty()) {
6966
const unsigned int index = unused[unused.size() - 1];
7067
active[index] = DLdata(dlist, m);

rts/Lua/LuaFBOs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class LuaFBOs {
1616
LuaFBOs() { fbos.reserve(8); }
1717
~LuaFBOs();
1818

19+
void Clear() { fbos.clear(); }
20+
1921
struct FBO {
2022
void Init(lua_State* L);
2123
void Free(lua_State* L);

rts/Lua/LuaParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ LuaParser::LuaParser(const string& _textChunk, const string& _accessModes, const
8585

8686
LuaParser::~LuaParser()
8787
{
88+
// prevent crashes in glDelete* calls since LuaParser
89+
// might be constructed by multiple different threads
90+
D.Clear();
91+
8892
if (L != nullptr)
8993
LUA_CLOSE(&L);
9094

rts/Lua/LuaRBOs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class LuaRBOs {
1616
LuaRBOs() { rbos.reserve(8); }
1717
~LuaRBOs();
1818

19+
void Clear() { rbos.clear(); }
20+
1921
static bool PushEntries(lua_State* L);
2022

2123
struct RBO;

rts/Lua/LuaShaders.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class LuaShaders {
1919
LuaShaders();
2020
~LuaShaders();
2121

22+
void Clear() {
23+
programs.clear();
24+
unused.clear();
25+
}
26+
2227
std::string errorLog;
2328

2429
GLuint GetProgramName(unsigned int progIdx) const;

rts/Lua/LuaTextures.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class LuaTextures {
2121
lastCode = 0;
2222
}
2323

24+
void Clear() {
25+
textureVec.clear();
26+
textureMap.clear();
27+
freeIndices.clear();
28+
}
29+
2430
struct Texture {
2531
Texture()
2632
: name(""), id(0), fbo(0), fboDepth(0),

0 commit comments

Comments
 (0)