草庐IT

c++ - Lua C API 内存泄漏? (valgrind)

coder 2024-02-05 原文

我正在尝试编写一个嵌入了 Lua 的 C 程序。而且,我尝试了一个非常简单的程序来启动,它只是创建 Lua 上下文,然后销毁它:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern "C" {
    #include <lua.h>
    #include <lauxlib.h>
    #include <lualib.h>
}

int main(int argc, char *argv[]) {
    lua_State *L = lua_open();
    luaL_openlibs(L);

    lua_close(L);
    fprintf(stderr, "%s: %d\n", __FILE__, __LINE__);
    return(0);
}

我是这样编译的:(我实际上使用的是 Torch7,所以..)

g++ -c -g3 -O2 -Wall -Werror -I/usr/local/torch/install/include/ -fPIC pure_lua_test.C -o pure_lua_test.o
g++ -g3 -O2 -Wall -Werror -I/usr/local/torch/install/include/ -fPIC -o pure_lua_test pure_lua_test.o -L/usr/local/torch/install/lib/ -lluajit

当我自己运行它时,它会打印

pure_lua_test.C: 16

正如预期的那样,(就在返回之前)。

但是,当我用 valgrind 运行它时,(如 valgrind ./pure_lua_test )

我明白了

==9165== Memcheck, a memory error detector
==9165== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9165== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==9165== Command: ./pure_lua_test
==9165== 
==9165== Invalid read of size 4
==9165==    at 0x4E9EE97: lua_pushcclosure (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165==    by 0x4EB4CDD: luaL_openlibs (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165==    by 0x400700: main (pure_lua_test.C:13)
==9165==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==9165== 
==9165== 
==9165== Process terminating with default action of signal 11 (SIGSEGV)
==9165==  Access not within mapped region at address 0x8
==9165==    at 0x4E9EE97: lua_pushcclosure (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165==    by 0x4EB4CDD: luaL_openlibs (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165==    by 0x400700: main (pure_lua_test.C:13)
==9165==  If you believe this happened as a result of a stack
==9165==  overflow in your program's main thread (unlikely but
==9165==  possible), you can try to increase the size of the
==9165==  main thread stack using the --main-stacksize= flag.
==9165==  The main thread stack size used in this run was 8388608.
==9165== 
==9165== HEAP SUMMARY:
==9165==     in use at exit: 0 bytes in 0 blocks
==9165==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==9165== 
==9165== All heap blocks were freed -- no leaks are possible
==9165== 
==9165== For counts of detected and suppressed errors, rerun with: -v
==9165== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

有人知道这是怎么回事吗?为什么在 valgrind 中使用 SIGSEGV?这是我应该担心的事情吗?基本上,我希望验证我为 Torch 编写的插件没有内存泄漏......但是,如果它失败了,那么,我有点卡住了。

最佳答案

这个问题的原因似乎是 Valgrind,而不是 LuaJIT。 Valgrind 是 blocking MAP_32BIT which breaks LuaJIT .为了演示,在 lua_State * L 上添加对 NULL 的检查,您会看到它在 Valgrind 下运行时为 NULL,而非 NULL同时定期运行。

这是我对您的示例所做的修改:

if(L == NULL) {
    printf("Could not create luaL_newstate()\n");
} else {
    luaL_openlibs(L);
    lua_close(L);
    printf("I can create luaL_newstate fine!\n");
}

当我正常运行时:

$ ./pure_lua_test 
I can create luaL_newstate fine!

但是当我在 Valgrind 下运行它时:

$ valgrind ./pure_lua_test
==8211== Memcheck, a memory error detector
==8211== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8211== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8211== Command: ./pure_lua_test
==8211== 
Could not create luaL_newstate()
==8211== 

GDB 还报告应用程序正常退出:

(gdb) run
Starting program: /tmp/pure_lua_test 
I can create luaL_newstate fine!
[Inferior 1 (process 8621) exited normally]

这是一个完整的 MCVE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern "C" {
        #include <lua.h>
        #include <lauxlib.h>
        #include <lualib.h>
}

int main(int argc, char *argv[]) {
    lua_State *L;
    L = luaL_newstate();

    if(L == NULL) {
        printf("Could not create luaL_newstate()\n");
    } else {
        luaL_openlibs(L);

        lua_close(L);
        printf("I can create luaL_newstate fine!\n");
    }

    return(0);
}

Here是一篇关于 MAP_32BIT 的好文章。希望这能以任何方式提供帮助。

关于c++ - Lua C API 内存泄漏? (valgrind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31955462/

有关c++ - Lua C API 内存泄漏? (valgrind)的更多相关文章

  1. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby-on-rails - Ruby 中的内存模型 - 2

    ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序

  4. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  5. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  6. 键删除后 ruby​​ 哈希内存泄漏 - 2

    你好,我无法成功如何在散列中删除key后释放内存。当我从哈希中删除键时,内存不会释放,也不会在手动调用GC.start后释放。当从Hash中删除键并且这些对象在某处泄漏时,这是预期的行为还是GC不释放内存?如何在Ruby中删除Hash中的键并在内存中取消分配它?例子:irb(main):001:0>`ps-orss=-p#{Process.pid}`.to_i=>4748irb(main):002:0>a={}=>{}irb(main):003:0>1000000.times{|i|a[i]="test#{i}"}=>1000000irb(main):004:0>`ps-orss=-p

  7. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  8. ruby-on-rails - HTTParty 的内存问题和下载大文件 - 2

    这会导致Ruby出现内存问题吗?我知道如果大小超过10KB,Open-URI会写入TempFile。但是HTTParty会在写入TempFile之前尝试将整个PDF保存到内存吗?src=Tempfile.new("file.pdf")src.binmodesrc.writeHTTParty.get("large_file.pdf").parsed_response 最佳答案 您可以使用Net::HTTP。参见thedocumentation(特别是标题为“流媒体响应机构”的部分)。这是文档中的示例:uri=URI('http://e

  9. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  10. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

随机推荐