lua监听内存释放
2016-12-13 17:44
225 查看
lua代码:
local dog = Dog.New()
dog:eat();
dog=nil;
collectgarbage()
c代码:
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#define MAX_PATH 260
class dog
{
public:
dog()
{
printf("constroctor\n");
memset(sName,0,MAX_PATH);
strcpy(sName,"Kaer");
}
void init()
{
printf("init\n");
memset(sName,0,MAX_PATH);
strcpy(sName,"Kaer");
}
void eat()
{
printf("%s eat\n",sName);
}
static int eat_api(lua_State *L)
{
dog *pDog = (dog*)lua_touserdata(L,-1);
pDog->eat();
return 0;
}
static int delete_dog(lua_State * L)
{
printf("dog is going to be deleted\n");
return 0;
}
static int NewDog(lua_State * L)
{
void *pMemory = lua_newuserdata(L,sizeof(dog));
// new(pMemory)dog;
//上面申请内存不会调用构造函数,因此我们这里给他调用初始化函数
dog *pDog = (dog*)pMemory;
pDog->init();
luaL_getmetatable(L,"Dog_Metetable");
lua_setmetatable(L,-2);
return 1;
}
static int Export(lua_State * L)
{
luaL_Reg api_c[] = {
{"New",NewDog}, //可以只是将new放在这个里面,其他的注册在元表中
{NULL,NULL}
};
luaL_register(L,"Dog",api_c);
//设置元表
luaL_newmetatable(L,"Dog_Metetable");
//将申请的在栈顶的metatable复制一份放在栈顶
lua_pushvalue(L,-1);
//将值设置到table中,并将上面的value出栈
lua_setfield(L,-2,"__index");
luaL_Reg api_m[] = {
{"eat", eat_api},
{NULL,NULL}
};
//这个NULL,表示栈顶的元表
luaL_register(L,NULL,api_m);
//设置垃圾回收提示
lua_pushcfunction(L, delete_dog);
lua_setfield(L,-2,"__gc");
return 0;
}
public:
char sName[MAX_PATH];
};
int _tmain(int argc, _TCHAR* argv[])
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
//注册导出类
lua_pushcfunction(L,dog::Export);
lua_pcall(L,0,0,0);
if(0 != luaL_dofile(L,"helloworld.lua"))
{
printf("%s",lua_tostring(L,-1));
}
system("pause");
return 0;
}
local dog = Dog.New()
dog:eat();
dog=nil;
collectgarbage()
c代码:
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#define MAX_PATH 260
class dog
{
public:
dog()
{
printf("constroctor\n");
memset(sName,0,MAX_PATH);
strcpy(sName,"Kaer");
}
void init()
{
printf("init\n");
memset(sName,0,MAX_PATH);
strcpy(sName,"Kaer");
}
void eat()
{
printf("%s eat\n",sName);
}
static int eat_api(lua_State *L)
{
dog *pDog = (dog*)lua_touserdata(L,-1);
pDog->eat();
return 0;
}
static int delete_dog(lua_State * L)
{
printf("dog is going to be deleted\n");
return 0;
}
static int NewDog(lua_State * L)
{
void *pMemory = lua_newuserdata(L,sizeof(dog));
// new(pMemory)dog;
//上面申请内存不会调用构造函数,因此我们这里给他调用初始化函数
dog *pDog = (dog*)pMemory;
pDog->init();
luaL_getmetatable(L,"Dog_Metetable");
lua_setmetatable(L,-2);
return 1;
}
static int Export(lua_State * L)
{
luaL_Reg api_c[] = {
{"New",NewDog}, //可以只是将new放在这个里面,其他的注册在元表中
{NULL,NULL}
};
luaL_register(L,"Dog",api_c);
//设置元表
luaL_newmetatable(L,"Dog_Metetable");
//将申请的在栈顶的metatable复制一份放在栈顶
lua_pushvalue(L,-1);
//将值设置到table中,并将上面的value出栈
lua_setfield(L,-2,"__index");
luaL_Reg api_m[] = {
{"eat", eat_api},
{NULL,NULL}
};
//这个NULL,表示栈顶的元表
luaL_register(L,NULL,api_m);
//设置垃圾回收提示
lua_pushcfunction(L, delete_dog);
lua_setfield(L,-2,"__gc");
return 0;
}
public:
char sName[MAX_PATH];
};
int _tmain(int argc, _TCHAR* argv[])
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
//注册导出类
lua_pushcfunction(L,dog::Export);
lua_pcall(L,0,0,0);
if(0 != luaL_dofile(L,"helloworld.lua"))
{
printf("%s",lua_tostring(L,-1));
}
system("pause");
return 0;
}
相关文章推荐
- Lua模块的加载与内存释放
- Lua中释放table占用内存的方法
- Cocos2d-Lua(Quick-Cocos2d-x)内存释放
- Cocos2d-Lua(Quick-Cocos2d-x)内存释放
- quick cocos2dx lua 内存释放
- 关于如何释放lua table占用的内存
- 经验总结25--释放内存的重要性
- Jni内存释放
- 在DLL中用CRT静态库申请内存,EXE释放是不行的
- Android下Activity结束(finish())之后没有释放内存问题的解决方法
- C++实现矩阵类型的基本操作:创建矩阵(M x N)、释放内存、获取某一元素的值、修改某一元素的值、乘法操作
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
- Linux释放内存小脚本
- 我必须在程序退出之前释放分配的所有内存吗?
- C++堆内存空间详解(释放内存、内存泄露)
- [转载]Lua 内存泄露举例
- /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题
- linux释放垃圾内存
- PHP的unset究竟会不会释放内存?
- 释放增加win2003系统的内存的方法让内存多空些出来