#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:
Blocking:

Description

This issue was already discussed here

http://www.rtems.com/ml/rtems-users/2004/october/msg00282.html

and here:

http://www.rtems.com/ml/rtems-users/2004/december/msg00004.html

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 Aug 24, 2007 at 12:05:08 AM.
Fix memory leak; use newlibc reclaim_reent.
PR#1246-newlib-1.15.0-rtems_stdio_cleanup.diff (4.6 KB) - added by strauman on Aug 24, 2007 at 12:21:02 AM.
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 Aug 24, 2007 at 12:05:08 AM by strauman

Fix memory leak; use newlibc reclaim_reent.

Changed on Aug 24, 2007 at 12:21:02 AM by strauman

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

comment:1 Changed on Jun 4, 2010 at 8:51:29 PM 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 Apr 1, 2014 at 11:25:14 AM 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 Nov 24, 2014 at 6:58:28 PM 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.