MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/tutorials/object_lifetime.cpp:7–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include <iostream>
6
7int main() {
8 sol::state lua;
9 lua.open_libraries(sol::lib::base);
10
11 lua.script(R"(
12 obj = "please don't let me die";
13 )");
14
15 sol::object keep_alive = lua["obj"];
16 lua.script(R"(
17 obj = nil;
18 function say(msg)
19 print(msg)
20 end
21 )");
22
23 lua.collect_garbage();
24
25 lua["say"](lua["obj"]);
26 // still accessible here and still alive in Lua
27 // even though the name was cleared
28 std::string message = keep_alive.as<std::string>();
29 std::cout << message << std::endl;
30
31 // Can be pushed back into Lua as an argument
32 // or set to a new name,
33 // whatever you like!
34 lua["say"](keep_alive);
35
36 return 0;
37}

Callers

nothing calls this directly

Calls 3

open_librariesMethod · 0.80
scriptMethod · 0.80
collect_garbageMethod · 0.80

Tested by

no test coverage detected