g2o icon indicating copy to clipboard operation
g2o copied to clipboard

g2o memory management

Open sebastiendalibard opened this issue 7 years ago • 7 comments

Hi,

g2o has a memory management pattern based on raw pointers, deleted by owner at object destruction. Although valid, this pattern is hard to maintain without making mistakes. There actually are some memory leaks in the current code, for example here where _algorithm is not freed before reassigned.

The canonical solution in modern C++ would be to express object ownership with smart pointers. In particular, I would expect g2o to make an efficient usage of std::unique_ptr. The code would be lighter, without having to call delete, and less error prone.

Would you be willing to integrate patches going in that direction?

Thanks for your feedback,

Sébastien

sebastiendalibard avatar Jul 04 '17 11:07 sebastiendalibard

👍 to this! Would also be happy of help pushing in that direction if @RainerKuemmerle agrees with the idea.

jlblancoc avatar Jul 05 '17 10:07 jlblancoc

Yes, I would appreciate contributions that incorporate smart pointer.

RainerKuemmerle avatar Jul 05 '17 18:07 RainerKuemmerle

Great! Would you like to create a new branch for this feature? In this way, PRs would go against it and could be extensively tested before really merging them to master...

jlblancoc avatar Jul 05 '17 21:07 jlblancoc

Don't know if it's of use, but I had started porting code to C++11 (c++11_stringent_tests branch). The changes I'd made so far were to change a lot of the iterators to use the : syntax and auto where possible to reduce the size of the code.

sjulier avatar Jul 08 '17 16:07 sjulier

Hey y'all, so I've already got a version of g2o with disabled implicit ownership of memory to allow batched allocations, which makes creation time and destruction time much faster so you basically have non-owning pointers throughout the code for edges and vertices, but unique_ptrs for owning pointers. I believe this is the kind of change you're imagining, and once I fix my auth issues with my work github account I can create a pull request for it (probably next week). How far along was your current change?

ghost avatar Jul 21 '17 17:07 ghost

Nevermind got my issues worked out haha :)

jumonatr avatar Jul 21 '17 17:07 jumonatr

So, how's it going on? The reason I am here is I encountered a problem when using g2o. I used smart pointers to manage vertex and edge pointers, and without any doubt it failed me.

zhangjun-xyz avatar Mar 05 '20 00:03 zhangjun-xyz