#1246 closed defect (fixed)

memory leak: newlib reent struct not cleaned up from libc_delete_hook

Reported by: strauman Owned by: Chris Johns
Priority: normal Milestone: 4.11
Component: fs Version: 4.11
Severity: normal Keywords:
Cc: chrisj@…, sebastian.huber@… Blocked By:


This issue was already discussed here


and here:


It would be better to use newlibc's reclaim_reent() routine to cleanup the
reent struct rather than replicating bits of that routine in the libc_delete_hook(). HOWEVER: newlibc (1.15) stdin/stdout/stderr FILE structs are
per-thread, not per-process and
reclaim_reent() walks fclose() (calling fflush()) on those three FILEs. These can be illegal operations since the device
driver may block in fflush() etc. but the delete_hook is executing from a dispatch disabled section (and possibly even a different thread context).

A better hack (because it still requires tampering with the reent struct)
than what's currently used would IMO be:

from libc_delete_hook()

set _REENT->stdin->_close = 0;

_REENT->stdin->_seek = 0;
_REENT->stdin->_flags&= ~SWR;

(same for stdout, stderr) so that the fclose/fflush operation caused
by reclaim_reent() doesn't cause any device I/O.

eventually, call reclaim_reent().

Other possibilities (involving newlib patching)

  • let newlib implement fpurge() and call prior to reclaim_reent() -- still requires avoiding device close operation.
  • create 'rtems_stdio_cleanup' function that does exactly what fclose does but omits the fflush() and device close operations. Then attach that 'rtems_stdio_cleanup' to _REENT->cleanup
  • let stdin/stdout/stderr file structs be per-process rather than per-thread objects (like all other FILEs) but this would probably require implementing locking

Attachments (2)

PR#1246-rtems-newlibc-reclaim_reent.diff (3.0 KB) - added by strauman on 08/24/07 at 00:05:08.
Fix memory leak; use newlibc reclaim_reent.
PR#1246-newlib-1.15.0-rtems_stdio_cleanup.diff (4.6 KB) - added by strauman on 08/24/07 at 00:21:02.
fix newlibc's reclaim_reent() [rtems only] so it can be used to plug memory leak

Download all attachments as: .zip

Change History (5)

Changed on 08/24/07 at 00:05:08 by strauman

Fix memory leak; use newlibc reclaim_reent.

Changed on 08/24/07 at 00:21:02 by strauman

fix newlibc's reclaim_reent() [rtems only] so it can be used to plug memory leak

comment:1 Changed on 06/04/10 at 20:51:29 by Chris Johns

Component: cpukitfilesystem
Milestone: 4.84.11
Owner: changed from Joel Sherrill to Chris Johns
Status: newassigned, chrisj@rtems.org
Version: 4.7HEAD

comment:2 Changed on 04/01/14 at 11:25:14 by Sebastian Huber

Resolution: fixed
Status: assignedclosed, sebastian.huber@embedded-brains.de

Fixed with Newlib 2.1.0, see also libtests/newlib01.

comment:3 Changed on 11/24/14 at 18:58:28 by Gedare Bloom

Version: HEAD4.11

Replace Version=HEAD with Version=4.11 for the tickets with Milestone >= 4.11

Note: See TracTickets for help on using tickets.