From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.187]) by sourceware.org (Postfix) with ESMTPS id D838E3851C09 for ; Sat, 5 Dec 2020 15:58:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D838E3851C09 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ritter-vogt.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=fabian@ritter-vogt.de Received: from linux-e202.suse.de ([79.198.107.251]) by mrelayeu.kundenserver.de (mreue012 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MjPYI-1kIFTU2Y94-00kzd5; Sat, 05 Dec 2020 16:58:50 +0100 From: Fabian Vogt To: newlib@sourceware.org, Dave Nadler Subject: Re: impure_data never reclaimed (unless you add the required cleanup code) Date: Sat, 05 Dec 2020 16:58:49 +0100 Message-ID: <4717243.TQ9fybG2G6@linux-e202.suse.de> In-Reply-To: References: <3052588.mabWh2cJZc@linux-e202.suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="ISO-8859-1" X-Provags-ID: V03:K1:pBEbJLMY+cR60NKmRAQhLnkfw+zgNXkFmsDv/SyewP9GGeocFN1 wtJpcxj/MsQAKQLokP1gGjFsrkdy7yzOU5KtRMhuW0Pew5Mf0qkRG56o/44EnysmRdgG39z 7Mia9vAZaDBknzP/FMsFb4+BZYYLL9+AztGRCKxNjGttlyPb+1w+4lpOXeORb1c4+QdFYCA glrY5OoMU42OrZlUUbUyA== X-UI-Out-Filterresults: notjunk:1;V03:K0:5SuadHivlOQ=:Gm9zMVP6gb33s0BMv13Xf9 VY7QFWZkIlsTiDQb2OcfrMtVqQnO5uZN81z19stSpp/QljY4QCgNs7HnG5ebBEKNnC6EKWEJI zod2hgcBJ1qs5sP/GLvmZGFjOEAQ91eiXGbo/oif05ze8/NTmQbfHmjStdEkDshVa93io1qWQ UdWQWgJ6Xw0kfWJntpaFGgZyQiiRcFtvpsjBOdYpsUW7Ai1HpEmJdMJzcFF1dTgA4+U8xKNcc aC+eRiSfMVE/pCpYdx2aKUNq5AjYPkJKuEtPTdqrE9QZlVJTDp+mqjZ4Yci+G0SRoCB72XY1X 9fCOSiYy8KOee1xUTbf6cXtecc9mseLsRnUff7OECdDEkN9Hoc4mZBSwOSKugaTaKikh+h20/ 7gDjgzXorQUIUPybITRR18WDxPYYgVMGuqzSmf6Dq/0uzgVqOXlLClzjGtG3Mibqn0TwbnVBF 1ssrijxdsA== X-Spam-Status: No, score=2.0 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Dec 2020 15:58:59 -0000 Hi, Am Samstag, 5. Dezember 2020, 16:48:57 CET schrieb Dave Nadler: > Hi Fabian - Yes, that is correct, you need that cleanup code. > However, you must also allocate (and clean up) the reentrancy structure > for each task/thread. > FreeRTOS correctly does this on task/thread creation and cleanup (with > #define configUSE_NEWLIB_REENTRANT 1). > Hope that helps! While searching for more info about this I found your related thread on this ML from some time ago, but I think it's not the same situation. newlib/libc/reent/impure.c has: static struct _reent __ATTRIBUTE_IMPURE_DATA__ impure_data = _REENT_INIT (impure_data); struct _reent *__ATTRIBUTE_IMPURE_PTR__ _impure_ptr = &impure_data; So the struct for the main (without thread support, the only) thread is allocated statically by newlib. It would be possible to do it like FreeRTOS and just allocate a new struct for _impure_ptr for the main thread, but that would leave impure_data unused and thus waste space and need more code on top. Cheers, Fabian > Best Regards, ,Dave > > On 12/5/2020 9:27 AM, Fabian Vogt wrote: > > Hi, > > > > I'm using newlib as libc on top of an OS which provides malloc and free. > > Threads aren't supported, but programs can be started and stopped. The used > > heap is shared between all programs, so leaked memory is lost forever. Thus > > it is important that after a clean exit, every allocation was properly freed. > > > > Currently this simple program leaks three allocations though: > > > > #include > > int main() > > { > > setbuf(stdout, NULL); // This would be properly freed, avoid distraction > > printf("This is 0.5 as a float: %f\n", 0.5f); > > } > > > > Those are from Balloc, which allocates a list also for later use. > > FWICT, _reclaim_reent takes care of freeing the list and its contents, but: > > - _reclaim_reent is never called for _impure_ptr (== &impure_data) > > - _reclaim_reent does nothing if impure_ptr is passed > > > > Is the OS glue code supposed to do something like this in _exit? > > > > struct _reent *global_reent = _impure_ptr; > > _impure_ptr = NULL; > > _reclaim_reent(global_reent); > > > > With that the leaks are gone, but it seems a bit odd to me. > > > > Thanks, > > Fabian