micro-os-plus / micro-os-plus-iii

The portable part of µOS++ IIIe (an xpm/npm package)
http://micro-os-plus.github.io
MIT License
115 stars 17 forks source link

Clarification on free() and operator delete() without length information #52

Open LnnrtS opened 4 years ago

LnnrtS commented 4 years ago

When using first-fit-top as the default allocator I am seeing memory leaks when using allocating stl objects (like std::vector). The reason is that those objects are calling operator delete without length information. The call basically gets discarded in https://github.com/micro-os-plus/micro-os-plus-iii/blob/1ea30f65acfc9b25e8bf6558035e5dd1bc3b9127/src/libcpp/new.cpp#L339

How is this supposed to work? The only option I see is using a default allocator that can deallocate without being given the size information. But that would rule out using any of the three memory resources provided with micro os.

Or should I always have to specify an allocator when using stl objects, like std::vector<T,allocator_stateless_polymorphic_synchronized<T, scheduler::lockable, get_default_resource>> But what is the use of the definitions of operator delete then?

free() does the same btw https://github.com/micro-os-plus/micro-os-plus-iii/blob/1ea30f65acfc9b25e8bf6558035e5dd1bc3b9127/src/libc/stdlib/malloc.cpp#L369

ilg-ul commented 4 years ago

Hi @LnnrtS, thank you for raising this issue. It looks like I overlooked some of the details.

The memory allocators will be reworked in the next release of µOS++, to match the new standard definitions. Work is scheduled to start in a few weeks time, after all build tools will be ready.

For now, please feel free to use any workaround that works for you. At first sight I would make a shorter alias of the allocator, and pass it to stl objects.

Please leave this issue open, and let me know your workaround.

Any other comments and suggestions for improving µOS++ are welcomed, here or in the project forum.

LnnrtS commented 4 years ago

Thank you for your quick response.

I ended up defining my custom container types as the std ones allocator type set to the one provided by µOS++.

Just for curiosity: Could you elaborate a bit more on how the standard definitions changed? I see that std::allocator<T> is still calling operator delete(ptr). So the memory resources need to be changed to remember the size to deallocate for a specific pointer?