Public members which maybe null. was: Failed to Call Member function of Shared_ptr Instance

Issue #5 resolved
Jing GENG
created an issue

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:

#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:

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:

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!

Comments (5)

  1. Liam Devine repo owner

    The first problem you identify, to be honest, is something I have not considered. Before the introduction of the maybe_null trait, NULL pointers were not allowed inside of OOLua. The introduction of the trait allows pointers and shared pointers to be comparible to NULL when they are function returns. Seeing as public members are proxied with functions in C++, maybe this trait should also be allowed on public members returned to Lua? I will think about this.

    The second issue from the same snippet of code, requires me to inspect it at a desktop machine as I am not sure why you would see this error.

    The third issue is as the error message suggests, the function index given to the function caller is out of bounds. run_chunk runs a chunks and returns any results, load_chunk loads a chunk and returns the chunk as a function. It seems you are confusing the two.. Either you call run_chunk and return the function or you call load_chunk and then for either you use the stack index for the function call.

  2. Jing GENG reporter

    Thank you for your remarks. Actually we found that if shared_ptr was used, "OOLUA_USE_SHARED_PTR=1" should be added to the "Preprocessor" option of "oolua" project. After rebuild "oolua" project, everything works fine. Anyway, thank you again for your great patience and advice!

  3. Log in to comment