starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.75k stars 387 forks source link

怀疑有一个内存泄漏问题。 #18

Closed woodzong closed 6 years ago

woodzong commented 6 years ago

重现方法如下: 临时加了一个"Lpb_loadfileTest" 函数(修改自Lpb_loadfile). 方便测试

//==in pb.c file========================== int Lpb_loadfileTest(lua_State L, const char filename) { pb_State S = default_state(L); //const char filename = luaL_checkstring(L, 1); size_t size; pb_Buffer b; pb_SliceExt s; int ret; FILE fp = fopen(filename, "rb"); if (fp == NULL) return luaL_fileresult(L, 0, filename); pb_initbuffer(&b); do { void d = pb_prepbuffsize(&b, BUFSIZ); if (d == NULL) { fclose(fp); return luaL_error(L, "out of memory"); } size = fread(d, 1, BUFSIZ, fp); pb_addsize(&b, size); } while (size == BUFSIZ); fclose(fp); s = lpb_initext(pb_result(&b)); ret = pb_load(S, &s.base); pb_resetbuffer(&b); lua_pushboolean(L, ret == PB_OK); lua_pushinteger(L, lpb_offset(&s)); return 2; }

//==================== in main.c file======================

include

include

include <sys/stat.h>

include <sys/time.h>

include

include

include

include

include "lua.h"

include "lauxlib.h"

include "lualib.h"

extern int Lpb_loadfileTest(lua_State L, const char fileName);

int main() { int ret = 0; int i = 0; for( i = 0; i < 1000000; i++ ) { lua_State *L = luaL_newstate(); luaL_openlibs( L ); luaopen_pb( L );

    Lpb_loadfileTest( L, "C:/u2test2.pb" );

    lua_close( L );

    usleep( 1000 );

    if( i % 1000 == 0 )
    {
        printf( "RunIndex:%d\n", i );   
    }
}
return 0;

}

/////////////////////////////////////////////////////// 编译后执行, 进程内存会不停地涨, 注释掉 Lpb_loadfileTest 的调用就不会涨。 换大的 pb 文件会更明显。

starwing commented 6 years ago

想问下你明显Linux环境是怎么打开C盘文件的?

woodzong commented 6 years ago

我用MINGW环境编译的.

woodzong commented 6 years ago

在linux 下也可以重现. 跑起来后. 进程的 RES 会不停地增长。

woodzong commented 6 years ago

linux 下 是 gcc 4.9.2 LuaJIT-2.1.0-beta2

woodzong commented 6 years ago

学习了一下源代码。 好像是因为 pb_resizetable 里 nt.hash = (pb_Entry*)malloc(nt.lastfree); 的内存没有被释放引起的。 主动调用了pbL_delete 后, 还会出现. 好像是因为 pb_freetable 里, 没有遍历table 嵌套的table引起的。 不知道理解得对不对。 求作者大力指导 :)

starwing commented 6 years ago

我做了个新提交,你看看内存问题还存在吗?

woodzong commented 6 years ago

更新了新版本. 问题解决了. 作者神速!!