From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Interfaces.C.Strings chars_ptr memory management strategy Date: Mon, 4 Jun 2018 09:06:54 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <6ff31dc6-31e7-4755-bda0-1b53fa02f31f@googlegroups.com> <735fd76c-c0a7-4e8c-a534-857772cada9b@googlegroups.com> <8816050d-27e6-42e2-b6e8-fd85b31e8e04@googlegroups.com> NNTP-Posting-Host: MyFhHs417jM9AgzRpXn7yg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:52911 Date: 2018-06-04T09:06:54+02:00 List-Id: On 2018-06-03 10:03 PM, ytomino wrote: > On Monday, June 4, 2018 at 4:33:20 AM UTC+9, Dmitry A. Kazakov wrote: >> On 2018-06-03 20:31, ytomino wrote: >>> Perhaps, malloc is better than New_String in this case. >>> >>> function malloc (s : Interfaces.C.size_t) return Interfaces.C.Strings.chars_ptr >>> with Import, Convention => C; >> >> I had a case when that caused the application crashed. >> >> I guess it was because of mixed Visual Studio and GCC run-times. The >> pointer returned by the malloc from one was freed in a third-party C >> library by another. >> >> -- >> Regards, >> Dmitry A. Kazakov >> http://www.dmitry-kazakov.de > > What!? > > New_String calls malloc in the end, too, in mingw runtime. > (It calls Memory_Alloc, Memory_Alloc is _gnat_malloc, and __gnat_malloc calls malloc.) > https://gcc.gnu.org/svn/gcc/trunk/gcc/ada/libgnat/i-cstrin.adb > https://gcc.gnu.org/svn/gcc/trunk/gcc/ada/libgnat/s-parame.ads > https://gcc.gnu.org/svn/gcc/trunk/gcc/ada/libgnat/s-memory__mingw.adb > > If Interfaces.C.Strings.Free (malloc) is crashed, New_String would be same. No, not this free but the one called from the third-party library because the pointer was passed there to handle. Of course it is safe to call malloc-free or New_String-Free pairs. Other combinations can be unsafe. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de