test_super_leak.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright Daniel Wallin 2009. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. struct Foo
  7. {
  8. static std::size_t count;
  9. Foo()
  10. {
  11. ++count;
  12. }
  13. ~Foo()
  14. {
  15. --count;
  16. }
  17. };
  18. std::size_t Foo::count = 0;
  19. void test_main(lua_State* L)
  20. {
  21. using namespace luabind;
  22. module(L) [
  23. class_<Foo>("Foo")
  24. .def(constructor<>())
  25. ];
  26. // This used to leak the last created instance in one of the
  27. // upvalues to the "super" function.
  28. disable_super_deprecation();
  29. DOSTRING(L,
  30. "class 'Bar' (Foo)\n"
  31. "function Bar.__init(self)\n"
  32. " Foo.__init(self)\n"
  33. "end\n"
  34. );
  35. DOSTRING(L,
  36. "x = Bar()\n"
  37. );
  38. assert(Foo::count == 1);
  39. DOSTRING(L,
  40. "x = nil\n"
  41. );
  42. lua_gc(L, LUA_GCCOLLECT, 0);
  43. assert(Foo::count == 0);
  44. }