[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: issue following array example in pil chapter 28.2
- From: David Rio <driodeiros@...>
- Date: Sun, 19 Feb 2012 19:02:48 -0600
Tet me try to simplify the amount of code necessary to reproduce
the issue:
--------------------$ cat compile.sh
#!/bin/bash
rm -f *.o *.so
gcc -g -c ldrdlib.c
gcc -g -Wall -shared -llua -o mylib.so ldrdlib.o
-------------------$ cat ldrdlib.c
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int l_sin (lua_State *L) {
double d = luaL_checknumber(L, 1);
lua_pushnumber(L, sin(d));
return 1; /* number of results */
}
static const struct luaL_reg mylib [] = {
{"l_sin", l_sin},
{NULL, NULL} /* sentinel */
};
int luaopen_mylib (lua_State *L) {
luaL_newmetatable(L, "LuaBook.array"); // THis is the line that breaks
luaL_openlib(L, "mylib", mylib, 0);
return 1;
}
-----------------$ cat play.lua
#!/usr/bin/env lua
require 'mylib'
print(mylib.l_sin(0.5))
And here is how I run it and the error:
$ ./compile.sh && ./lua-5.1.4/src/lua play.lua
0.4794255386042
lua(88449) malloc: *** error for object 0x102f22380: pointer being
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
This error pops up when I introduce this line of code in the c library
to create a new table:
luaL_newmetatable(L, "LuaBook.array");
Thanks,
-drd