april-org / april-ann

APRIL-ANN toolkit (A Pattern Recognizer In Lua with ANNs). This toolkit incorporates ANN algorithms (as dropout, stacked denoising auto-encoders, convolutional NNs), with other pattern recognition methods as HMMs among others. Additionally, in experimental stage, it is possible to perform automatic differentiation, for advanced ML research.
http://april-org.github.io/april-ann/
Other
74 stars 12 forks source link

Smart pointers, Lua calls and protected calls #179

Open pakozm opened 8 years ago

pakozm commented 8 years ago

In order to allow capturing errors in Lua side (via pcall or xpcall), objects allocated in C++ should be smart enough to free all pending memory in the moment of the error. LuaPkg and APRIL-ANN have been changed to implement this procedure in some automatic way, LuaPkg wraps every C++ call using a try/catch block, and APRIL-ANN macros ERROR_EXIT throw an exception consisting in a simple char * message. This procedure allows to execute automatic C++ cleanup when the exception is thrown followed by the longjmp due to Lua errors. Our problem is that not all C++ objects are error-safety, that is, some of them allocate memory in its methods and in case of error the memory stills allocated. A simple way to face this problem is to wrap all C++ allocations using AprilUtils::SharedPtr or AprilUtils::UniquePtr, so in case of error the allocated memory would freed automatically.