Lua 5.2 resume is crashing

the title tells everything...
the crash place is in "ala_lua_co.h" header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<cstdio>
#include<ala_lua.h>

extern"C"{
    void __stdcall Sleep(unsigned long);
};



int main(){
    lua_State*L=luaL_newstate();
        luaL_openlibs(L);
        luaA_libs(L);

    lua_State*T=lua_newthread(L);
        //lua_remove(L,lua_gettop(L));
        luaL_loadfile(T,"c:/clua.lua");
        co_add(T);
        lua_resume(T,NULL,0);

    while(true){
        co_update();
        Sleep(1);
    };

    lua_close(L);

    gets(new char[1]);

    return 0;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef ALA_LUA_H
#define ALA_LUA_H

#include<ala_lua_co.h>

static int luaA_sleep(lua_State*handle){
    lua_yield(handle,0);
    return 0;
};

static int luaA_rthread(lua_State*handle){
    if(lua_isthread(handle,1)){
        co_resume(lua_tothread(handle,1));
    };
    return 0;
};

static int luaA_mthread(lua_State*handle){
    if(lua_isfunction(handle,1)){
        lua_State*thread=lua_newthread(handle);

        lua_pushvalue(handle,1);
        lua_xmove(handle,thread,1);

        return 1;
    };
    return 0;
};

void luaA_libs(lua_State*handle){
    lua_register(handle,"mthread",luaA_mthread);
    lua_register(handle,"rthread",luaA_rthread);
    lua_register(handle,"sleep",luaA_sleep);
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef ALA_LUA_CO_H
#define ALA_LUA_CO_H

#include<vector>
#include<stdio.h>
#include<lua.hpp>

std::vector<lua_State*>co_threads;

void co_update();
void co_add(lua_State*);
void co_resume(lua_State*);

void co_report(lua_State*thread){
    if(lua_status(thread)==LUA_ERRRUN){
        printf("ERROR: %s",lua_tostring(thread,-1));
        lua_pop(thread,-1);
    };
};

void co_resume(lua_State*thread){
    switch(lua_status(thread)){
        case LUA_OK:{//never resumed thread or running or dead
            if(lua_gettop(thread)!=0){//surely not dead thread and not running thread
                if(lua_resume(thread,NULL,0)==LUA_YIELD){//crash maybe here
                    co_add(thread);
                }else{
                    co_report(thread);
                };
            };
            return;
        };
        case LUA_YIELD:{
            lua_resume(thread,NULL,0);
            return;
        };
        default:{
            return;
        };
    };
};

void co_add(lua_State*thread){
    co_threads.push_back(thread);
};

void co_update(){
    if(co_threads.empty())return;
    size_t i=co_threads.size();
    while(i>0){
        --i;

        lua_State*thread=co_threads[i];

        const int status(lua_status(thread));

        if(status==LUA_YIELD){
            lua_resume(thread,NULL,0);
            co_report(thread);
        }else{
            co_report(thread);
            co_threads.erase(co_threads.begin()+i);
        };
    };
};

#endif 



1
2
3
4
5
6
7
8
9
--lua source code

for thread_id=1,100 do
	for i=1,100 do coroutine.yield();end;
	rthread(mthread(function()
		for i=1,1000 do coroutine.yield();end;
		print(thread_id);
	end));
end;
Last edited on
I solved...

the problem was about collect garbage... so I just turned of collect garbage each "threads"
Topic archived. No new replies allowed.