[oolua] Issue #5: Failed to Call Member function of Shared_ptr Instance (liamdevine/oolua)

  • From: "Jing GENG" <issues-reply@xxxxxxxxxxxxx>
  • To: oolua@xxxxxxxxxxxxx
  • Date: Fri, 25 Jul 2014 10:20:17 -0000

New issue 5: Failed to Call Member function of Shared_ptr Instance
https://bitbucket.org/liamdevine/oolua/issue/5/failed-to-call-member-function-of

Jing GENG:

I'm using OOLua 2 downloaded from the main branch several days ago, and vs2008 
as the compiler. It seems that errors are all related to memory access.  The 
example is as following:


```
#!c++
#include "oolua.h"
#include <iostream>

class testForShare
{
public:
        OOLUA_SHARED_TYPE<testForShare> t_shared;
        testForShare():count(0)
        {}
        void do_something()
        {
                std::cout << "It is doing something.\n";
        }
private:
        int count;

};

OOLUA_PROXY(testForShare)
OOLUA_MGET(t_shared)
OOLUA_MFUNC(do_something)
OOLUA_PROXY_END

OOLUA_EXPORT_FUNCTIONS(testForShare,do_something)
OOLUA_EXPORT_FUNCTIONS_CONST(testForShare,get_t_shared)

int main()
{

        OOLUA::Script vm;
        vm.register_class<testForShare>();
        OOLUA_SHARED_TYPE<testForShare>t(new testForShare);
        vm.run_chunk("return function(obj)"
                //"obj:get_t_shared()"  // the 1st mistake may happen here if 
not annotated
                "obj:do_something()"
                "end");
        bool result = vm.call(1,t); // the 2nd mistake may happen here
        //std::cout << OOLUA::get_last_error(vm) << std::endl;  //error message 
threw if this line is added

        getchar();
        return 0;
}
```
# The 1st mistake #  may happen if the line is not annotated. The error message 
is like 
     "First-chance exception at 0x0015e1ab in privacy.exe: 0xC0000005: Access 
violation     reading location 0x00000008.
    Unhandled exception at 0x0015e1ab in privacy.exe: 0xC0000005: Access 
violation reading location 0x00000008". The project breaks at line 146, 
class_from_stack.cpp. 

# The 2nd mistake # will not cause runtime error but some message when I try to 
close the exe.

"Windows has triggered a breakpoint in privacy.exe.

This may be due to a corruption of the heap, which indicates a bug in 
privacy.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while privacy.exe has focus.

The output window may have more diagnostic information."


When the "get_last_error" function is used, the message from OOLua is:


"[string "return function(obj)obj:do_something()end"]:1: Tried to pull a none 
con
stant "testForShare" pointer from a const pointer
stack traceback:
        [C]: in function 'do_something'
        [string "return function(obj)obj:do_something()end"]:1: in function <[st
ring "return function(obj)obj:do_something()end"]:1>"


I've added "OOLUA_USE_SHARED_PTR=1" to "C/C++ ->Preprocessor" option in the 
project property. 

# Besides #, when I try to use an instance of an ordinary class to call a 
member function, there is also a mistake.

The class is exported to lua as following:

```
#!c++
class Apple
{       
public:
        Apple(void):
          m_weight(200),
                  m_color("red"),
                  m_clean(false)        
          {
                  std::cout << "Apple constructed successfully!" <<std::endl;
          }
          ~Apple(void)
          {
                  std::cout << "Apple destructed." <<std::endl;
          }
          void cleanApple()
          {
                  m_clean = true;
                  std::cout <<"The  apple is clean now !"<<std::endl;
          }
          double eatApple(double bitten)
          {     
                  m_weight -= bitten;
                  return m_weight;
          }
          static double anyCFunc (double x)
          {
                  x += 1;
                  std::cout << x <<     std::endl;
                  return x;
          }
          double getWeight()
          {
                  return m_weight;
          }
private:
        double m_weight;
        std::string m_color;
        bool m_clean;
};

OOLUA_PROXY(Apple)
        OOLUA_MFUNC(cleanApple)
        OOLUA_MFUNC(eatApple)
        OOLUA_MFUNC(getWeight)
        OOLUA_SFUNC(anyCFunc)   //This function will not be exported and needs 
to be registered with OOLua see OOLUA::register_class_static
OOLUA_PROXY_END

OOLUA_EXPORT_FUNCTIONS(Apple,cleanApple,eatApple,getWeight)
OOLUA_EXPORT_FUNCTIONS_CONST(Apple)

```

In main function the instance is used like:

```
#!c++

OOLUA::Script s_apple;
        Apple apple;
        s_apple.register_class<Apple>();
        
s_apple.register_class_static<Apple>("anyCFunc",OOLUA::Proxy_class<Apple>::anyCFunc);//register
 static function in Lua

        /*s_apple.run_chunk(
                "a=Apple.new()"
                "a:cleanApple()"
                "w = a:getWeight()"
                "print(w)"
                "w = a:eatApple(20)"
                "print(w)"
                "print('Hi!')");*/ // This part works fine

        s_apple.run_chunk(
                "function func(a)"
                "a:cleanApple()"
                "w = a:getWeight()"
                "print(w)"
                "w = a:eatApple(20)"
                "print(w)"
                "print('Hi!')"
                "end");

        s_apple.call(1,&apple);
        std::cout << OOLUA::get_last_error(s_apple) << std::endl;

```

The error message is:

"Assertion failed: !(top == 0 || func_index == 0|| func_index > top || 
-func_inde
x > top) && "Out of bounds index", file .\src\oolua_function.cpp, line 226"

Basically I have no idea why these happen and how can I fix it. Any idea or 
suggestion is welcome. 

Thanks a lot! 





Other related posts:

  • » [oolua] Issue #5: Failed to Call Member function of Shared_ptr Instance (liamdevine/oolua) - Jing GENG