Instantiating a C++ class in Lua

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

Instantiating a C++ class in Lua

eomer-2
This is completely a basic sort of issue, but I can't seem to see why the following does not work.

I have created a simple class. The class is exposed to luabind. I then create an instance of that class in Lua and I get an error.

I am using vc2003/WinXP with Lua 5.0 and LuaBind Beta 6.

The error displayed at runtime is at dbgdel.cpp (Line 52). The assertion failed: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). This occurs at the line where the call to dostring is made, but through debugging, I see that the constructor for class C has not been called.

Thanks in advance.

Chris

-------------------------------------------

// lua / luabind
extern "C"
{
 #include <lua.h>
 #include <lualib.h>
 #include <lauxlib.h>
}

// luabind
#include "luabind/luabind.hpp"

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class C
{
public:
        C()
        {
                mCount = 1;
        }

        int Count()
        {
                return mCount;
        }
private:
        int mCount;
};

int main()
{
        // Create a new lua state
        lua_State *pMyLuaState = lua_open();

        // Connect LuaBind to this lua state
        luabind::open(pMyLuaState);

        lua_baselibopen(pMyLuaState);
        lua_iolibopen(pMyLuaState);
        lua_strlibopen(pMyLuaState);
        lua_mathlibopen(pMyLuaState);


        // Define a lua function that we can call
        lua_dostring(
                pMyLuaState,
                "function add(first, second)\n"
                "  return first + second\n"
                "end\n"
        );

        luabind::module(pMyLuaState) [
        luabind::class_<C>("C")
        .def("Count", &C::Count)
        ];

        // This is where the error occurs
        lua_dostring(
        pMyLuaState,
        "someC = C()"
        );

        cout << "Result: "
        << luabind::call_function<int>(pMyLuaState, "add", 2, 3, 4)
        << endl;

        lua_close(pMyLuaState);
}

-----------------------------------------------------


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Instantiating a C++ class in Lua

George Faraj
Hey,
You have to register C's constructor,

>         luabind::module(pMyLuaState) [
>         luabind::class_<C>("C")
           .def(constructor<>())                         // add this line
>         .def("Count", &C::Count)
>         ];

Regards


On 1/16/06, [hidden email] <[hidden email]> wrote:

> This is completely a basic sort of issue, but I can't seem to see why the following does not work.
>
> I have created a simple class. The class is exposed to luabind. I then create an instance of that class in Lua and I get an error.
>
> I am using vc2003/WinXP with Lua 5.0 and LuaBind Beta 6.
>
> The error displayed at runtime is at dbgdel.cpp (Line 52). The assertion failed: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). This occurs at the line where the call to dostring is made, but through debugging, I see that the constructor for class C has not been called.
>
> Thanks in advance.
>
> Chris
>
> -------------------------------------------
>
> // lua / luabind
> extern "C"
> {
>  #include <lua.h>
>  #include <lualib.h>
>  #include <lauxlib.h>
> }
>
> // luabind
> #include "luabind/luabind.hpp"
>
> #include <iostream>
> #include <string>
> #include <vector>
> using namespace std;
>
> class C
> {
> public:
>         C()
>         {
>                 mCount = 1;
>         }
>
>         int Count()
>         {
>                 return mCount;
>         }
> private:
>         int mCount;
> };
>
> int main()
> {
>         // Create a new lua state
>         lua_State *pMyLuaState = lua_open();
>
>         // Connect LuaBind to this lua state
>         luabind::open(pMyLuaState);
>
>         lua_baselibopen(pMyLuaState);
>         lua_iolibopen(pMyLuaState);
>         lua_strlibopen(pMyLuaState);
>         lua_mathlibopen(pMyLuaState);
>
>
>         // Define a lua function that we can call
>         lua_dostring(
>                 pMyLuaState,
>                 "function add(first, second)\n"
>                 "  return first + second\n"
>                 "end\n"
>         );
>
>         luabind::module(pMyLuaState) [
>         luabind::class_<C>("C")
>         .def("Count", &C::Count)
>         ];
>
>         // This is where the error occurs
>         lua_dostring(
>         pMyLuaState,
>         "someC = C()"
>         );
>
>         cout << "Result: "
>         << luabind::call_function<int>(pMyLuaState, "add", 2, 3, 4)
>         << endl;
>
>         lua_close(pMyLuaState);
> }
>
> -----------------------------------------------------
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Instantiating a C++ class in Lua

eomer-2
George,

Thank you for the help. I was so focused on certain details that I wasn't able to see that.

My apologies.

Chris

----- Original Message -----
From: George Faraj <[hidden email]>
Date: Tuesday, January 17, 2006 1:15 am
Subject: Re: [luabind] Instantiating a C++ class in Lua

> Hey,
> You have to register C's constructor,
>
> >         luabind::module(pMyLuaState) [
> >         luabind::class_<C>("C")
>           .def(constructor<>())                         // add
> this line
> >         .def("Count", &C::Count)
> >         ];
>
> Regards
>
>
> On 1/16/06, [hidden email] <[hidden email]> wrote:
> > This is completely a basic sort of issue, but I can't seem to see
> why the following does not work.
> >
> > I have created a simple class. The class is exposed to luabind. I
> then create an instance of that class in Lua and I get an error.
> >
> > I am using vc2003/WinXP with Lua 5.0 and LuaBind Beta 6.
> >
> > The error displayed at runtime is at dbgdel.cpp (Line 52). The
> assertion failed: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). This
> occurs at the line where the call to dostring is made, but through
> debugging, I see that the constructor for class C has not been called.
> >
> > Thanks in advance.
> >
> > Chris
> >
> > -------------------------------------------
> >
> > // lua / luabind
> > extern "C"
> > {
> >  #include <lua.h>
> >  #include <lualib.h>
> >  #include <lauxlib.h>
> > }
> >
> > // luabind
> > #include "luabind/luabind.hpp"
> >
> > #include <iostream>
> > #include <string>
> > #include <vector>
> > using namespace std;
> >
> > class C
> > {
> > public:
> >         C()
> >         {
> >                 mCount = 1;
> >         }
> >
> >         int Count()
> >         {
> >                 return mCount;
> >         }
> > private:
> >         int mCount;
> > };
> >
> > int main()
> > {
> >         // Create a new lua state
> >         lua_State *pMyLuaState = lua_open();
> >
> >         // Connect LuaBind to this lua state
> >         luabind::open(pMyLuaState);
> >
> >         lua_baselibopen(pMyLuaState);
> >         lua_iolibopen(pMyLuaState);
> >         lua_strlibopen(pMyLuaState);
> >         lua_mathlibopen(pMyLuaState);
> >
> >
> >         // Define a lua function that we can call
> >         lua_dostring(
> >                 pMyLuaState,
> >                 "function add(first, second)\n"
> >                 "  return first + second\n"
> >                 "end\n"
> >         );
> >
> >         luabind::module(pMyLuaState) [
> >         luabind::class_<C>("C")
> >         .def("Count", &C::Count)
> >         ];
> >
> >         // This is where the error occurs
> >         lua_dostring(
> >         pMyLuaState,
> >         "someC = C()"
> >         );
> >
> >         cout << "Result: "
> >         << luabind::call_function<int>(pMyLuaState, "add", 2, 3, 4)
> >         << endl;
> >
> >         lua_close(pMyLuaState);
> > }
> >
> > -----------------------------------------------------
> >
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: Splunk Inc. Do you grep
> through log files
> > for problems?  Stop!  Download the new AJAX search engine that makes
> > searching your log files as easy as surfing the  web.  DOWNLOAD
> SPLUNK!> http://sel.as-
> us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642>
> _______________________________________________> luabind-user
> mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through
> log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD
> SPLUNK!http://sel.as-
> us.falkag.net/sel?cmdlnk&kid3432&bid#0486&dat1642_______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user