issue following array example in pil chapter 28.2

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

issue following array example in pil chapter 28.2

David Rio
Hi,

I was going over pil's chapter 28. After creating the array library
described in
28.1 I moved ahead and set a metatable for all the new userdatums of type array
so we avoid processing things that are not arrays.

The first thing to do is create the metatable within luaopen_array so the
metatable exists.

When adding that function call:

luaL_newmetatable(L, "LuaBook.array"); // in luaopen_array()

I get an error from malloc because it is trying to free memory that has
not been allocated:

lua(73040) malloc: *** error for object 0x10e922380: pointer being
freed was not allocated

I tried to dynamically allocate the second parameter of
luaL_newmetatable but the
problem persists.

I am using lua 5.1.4 under osx.

Any help would be appreciated,
-drd

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

Sam Roberts
On Fri, Feb 17, 2012 at 5:44 PM, David Rio <[hidden email]> wrote:
> luaL_newmetatable(L, "LuaBook.array"); // in luaopen_array()
>
> I get an error from malloc because it is trying to free memory that has
> not been allocated:

Something is broken, you should post more code.

Cheers,
Sam

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

martinwguy
On 18 February 2012 03:05, Sam Roberts <[hidden email]> wrote:
> On Fri, Feb 17, 2012 at 5:44 PM, David Rio <[hidden email]> wrote:
>> luaL_newmetatable(L, "LuaBook.array"); // in luaopen_array()
>>
>> I get an error from malloc because it is trying to free memory that has
>> not been allocated:
>
> Something is broken, you should post more code.

Specifically, can you post a complete code example that reproduces the
problem whe you run it?
I don't feel like typinge in all of Chapter 28 myself to see whether I
can reproduce your problem

    M

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

David Rio
Thanks for the quick reply.

The c code: http://is04607.com/tmp/ldrdlib.c.txt
The lua code that uses the previous library: http://is04607.com/tmp/play.c.lua
To compile the array lib: <a href="http://is04607.com:public_html/tmp/compile.sh.txt">http://is04607.com:public_html/tmp/compile.sh.txt

-drd

On Sat, Feb 18, 2012 at 07:18, Martin Guy <[hidden email]> wrote:

> On 18 February 2012 03:05, Sam Roberts <[hidden email]> wrote:
>> On Fri, Feb 17, 2012 at 5:44 PM, David Rio <[hidden email]> wrote:
>>> luaL_newmetatable(L, "LuaBook.array"); // in luaopen_array()
>>>
>>> I get an error from malloc because it is trying to free memory that has
>>> not been allocated:
>>
>> Something is broken, you should post more code.
>
> Specifically, can you post a complete code example that reproduces the
> problem whe you run it?
> I don't feel like typinge in all of Chapter 28 myself to see whether I
> can reproduce your problem
>
>    M
>

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

David Rio
And here you have a backtrace:

(gdb) bt
#0  0x00007fff8c5dc82a in __kill ()
#1  0x00007fff96f7ea9c in abort ()
#2  0x00007fff96fdd84c in free ()
#3  0x0000000100015591 in l_alloc (ud=0x13796, ptr=0x6, osize=0,
nsize=140734799803208) at lauxlib.c:631
#4  0x000000010000cdce in luaM_realloc_ (L=Cannot access memory at address 0x0
) at lmem.c:79
#5  0x00000001000112af in luaH_free (L=0x100100bc0, t=0x6) at ltable.c:376
#6  0x000000010000aa0c in freeobj [inlined] () at
/Users/drio/dev/luatools/lua-5.1.4/src/lgc.c:383
#7  0x000000010000aa0c in sweeplist (L=0x13796, p=0x6, count=0) at lgc.c:424
#8  0x000000010000aaba in luaC_freeall (L=0x100100bc0) at lgc.c:487
#9  0x0000000100010888 in close_state [inlined] () at
/Users/drio/dev/luatools/lua-5.1.4/src/lstate.c:108
#10 0x0000000100010888 in lua_close (L=0x100100bc0) at lstate.c:212
#11 0x00000001000009ef in main (argc=2, argv=0x7fff5fbff528) at lua.c:389

On Sat, Feb 18, 2012 at 08:59, David Rio <[hidden email]> wrote:

> Thanks for the quick reply.
>
> The c code: http://is04607.com/tmp/ldrdlib.c.txt
> The lua code that uses the previous library: http://is04607.com/tmp/play.c.lua
> To compile the array lib: <a href="http://is04607.com:public_html/tmp/compile.sh.txt">http://is04607.com:public_html/tmp/compile.sh.txt
>
> -drd
>
> On Sat, Feb 18, 2012 at 07:18, Martin Guy <[hidden email]> wrote:
>> On 18 February 2012 03:05, Sam Roberts <[hidden email]> wrote:
>>> On Fri, Feb 17, 2012 at 5:44 PM, David Rio <[hidden email]> wrote:
>>>> luaL_newmetatable(L, "LuaBook.array"); // in luaopen_array()
>>>>
>>>> I get an error from malloc because it is trying to free memory that has
>>>> not been allocated:
>>>
>>> Something is broken, you should post more code.
>>
>> Specifically, can you post a complete code example that reproduces the
>> problem whe you run it?
>> I don't feel like typinge in all of Chapter 28 myself to see whether I
>> can reproduce your problem
>>
>>    M
>>

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

David Rio
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

Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

Philipp Janda
On 20.02.2012 02:02, David Rio wrote:
> 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

I don't have access to a Mac, so I can't say for sure, but it seems your
gcc invocation is wrong. According to [1] it probably should be
something like:
     gcc -g -Wall -bundle -undefined dynamic_lookup -o mylib.so ldrdlib.o

   [1]: http://lua-users.org/wiki/BuildingModules

HTH,
Philipp


Reply | Threaded
Open this post in threaded view
|

Re: issue following array example in pil chapter 28.2

David Rio
Thanks Phillip! That was the issue.

-drd

On Sun, Feb 19, 2012 at 23:11, Philipp Janda <[hidden email]> wrote:

> On 20.02.2012 02:02, David Rio wrote:
>>
>> 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
>
>
> I don't have access to a Mac, so I can't say for sure, but it seems your gcc
> invocation is wrong. According to [1] it probably should be something like:
>    gcc -g -Wall -bundle -undefined dynamic_lookup -o mylib.so ldrdlib.o
>
>  [1]: http://lua-users.org/wiki/BuildingModules
>
> HTH,
> Philipp
>
>