I am working on embedding Lua in a C ++ project, which relies heavily on some inherited C codes. I have a few questions regarding garbage collection and variable areas.
Below are some snippets related to the following issues:
/* C side */ typedef struct { /* fields */ } Element; typedef struct { void * m_elems unsigned int size; } Container; // C API for managing Container and Element .... int allocContainer(Container*, unsigned int); void freeContainer(Container*); Element * getElemByIndex(unsigned int); // C++ side struct ElementWrapper { private: Element m_elem; public: /* field accessors (NO modifiers) */ }; class ContainerWrapper { private: ContainerWrapper m_cntnr; public: ContainerWrapper(int N); // allocates space for N elements ~ContainerWrapper(); // stack alloc'ed variable ElementWrapper getElementAtIndexStackAllocated(unsigned idx) const; // heap alloc'ed variable ElementWrapper * getElementAtIndexHeapAllocated(unsigned idx) const; }; -- Lua side wrapper = Container:new(10) for i =1,10 do -- get the ith element el_s = wrapper.getElementAtIndexStackAllocated(i) el_h = wrapper.getElementAtIndexHeapAllocated(i) -- do something with retrieved element .. end
. (, boost: shared_ptr), C / .., , , Lua, -. Lua ( tolua ++), C . , ++ Lua GC. , C mem - - , - , Lua GC (, , , ). , raw ptrs .
Lua , , - ( ). , el_h el_s , , local.
el_h
el_s
local
, , el_h, i= 1 , el_h , i= 2.
i
, tolua ++ , , , , C ++ .
, , tolua ++ , , . wrapper.getElementAtIndexStackAllocated() Lua, ++.
wrapper.getElementAtIndexStackAllocated()
, " ". , , , , . collectgarbage("collect")
collectgarbage("collect")
Source: https://habr.com/ru/post/1789185/More articles:how to control linux system on amazon ec2 without cloudwatch? - cloudПоразрядные операции помогут мне сериализовать некоторые bools? - c++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1789182/abstract-class-needs-to-know-all-extending-classes&usg=ALkJrhjkhpm8LoegXS-7EgNAYMyMG4PCtAОпределить в PHP скрипт при подключении к Интернету? - phpWhat is and why is there a difference between request.env ['CONTENT_TYPE'] and request.content_type? - content-typeWeb ui Drag and Drop DesignerIntegration with Twitter on iphone for reading public tweets / HashTags without using a username? - iphoneLocation Eclipse - res - androidHow to remove resizig handlers around image in IE using javascript? - javascriptiPhone - Help with MBProgressHUD, Sudzc, Web Services and NSThread - objective-cAll Articles