public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* Where stdin/stdout/stderr pointers are allocated
@ 2021-10-13  7:36 Rob Meades
  0 siblings, 0 replies; only message in thread
From: Rob Meades @ 2021-10-13  7:36 UTC (permalink / raw)
  To: newlib; +Cc: Andreas Anderberg

We run our own library of driver C code on several microcontroller platforms (e.g. STM32F4 and nRF5) where the SDK provided by the chip vendor uses newlib [pre-built by the vendor or by GCC] and FreeRTOS.  We use dynamically allocated tasks in the driver code and so, in our unit tests, we check for memory leaks to make sure we're shutting them down correctly.  FreeRTOS is sufficiently aware of newlib to call _reclaim_reent when a task exits.

The vendors do _not_ define _REENT_GLOBAL_STDIO_STREAMS and so each dynamic task ends up allocating file pointers for stdin/stdout/stderr.  As we understand it these are allocated in a _GLOBAL_REENT and not _REENT and, when a task exits, the file pointers are marked as free but not actually free'd so that they can be re-used later.

The problem then is that, since the file pointers are not being cleaned up when a dynamic task exits, our code "leaks" memory.  It is not _really_ a leak since the pointers are usable later by our driver code but it is taking memory from the customer's application that is not being returned.

QUESTION: is this how it is meant to work, stdin/stdout/stderr allocated in a global pool rather then in _REENT where FreeRTOS would free them on task exit?

If so our workaround, at least when unit testing, is to allocate a load of file pointers in a "preamble" test to take them out of our heap-checking equation.

Rob


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-13  7:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13  7:36 Where stdin/stdout/stderr pointers are allocated Rob Meades

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).