This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Hard fault in __free_r


Hello Trampas!

Your mistake here is that when using placement-new you MUST NOT call
delete. The memory is not from the heap, therefore trying to return it
to the heap (via calling delete, which in turn calls free) is just
completely wrong and forbidden by the language.

Your object must be explicitly destructed manually or via special
delete-function supplied to the smart pointer (I assume you talk about
things like std::unique_ptr<>). Therefore you either need to do:

ptrUart->~Uart();

when you are done, or - if you use smart pointers, create a no-op
function and supply that to the smart pointer during construction.

I have done something like that in my C++11 RTOS for ARM
microcontrollers. Here is the "no-op deleter" which I mentioned:

https://github.com/DISTORTEC/distortos/blob/master/include/distortos/in
ternal/memory/dummyDeleter.hpp

You may create a type alias for your smart pointer to make things
easier:

https://github.com/DISTORTEC/distortos/blob/master/include/distortos/in
ternal/scheduler/Stack.hpp#L37

And then you just assign both the pointer _and_ the deleter to the
smart pointer object, for example see the highlighted line:

https://github.com/DISTORTEC/distortos/blob/master/include/distortos/St
aticThread.hpp#L58

With this design the smart pointer can use any kind of memory - the
only difference is in the deleter you have to supply which will be
executed at the end. If you want to also use devices which are really
dynamically allocated (from the heap, with normal `new` or array-
`new`), then just use a different function, like this one:

https://github.com/DISTORTEC/distortos/blob/master/include/distortos/in
ternal/memory/storageDeleter.hpp

Usage example:

https://github.com/DISTORTEC/distortos/blob/master/include/distortos/in
ternal/scheduler/DynamicThreadBase.hpp#L214

BTW - if you use C++ and happen to work with an ARM Cortex-M
microcontroller, you may find my project quite interesting, at least I
hope so (;

Regards,
FCh


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]