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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,cb73ffe253a5caf1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Memory management clarification Date: Tue, 26 Jul 2005 17:39:21 +0200 Organization: CERN - European Laboratory for Particle Physics Message-ID: References: NNTP-Posting-Host: abpc10883.cern.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sunnews.cern.ch 1122392360 12410 (None) 137.138.37.241 X-Complaints-To: news@sunnews.cern.ch User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050724 Red Hat/1.7.10-1.1.3.1.SL3 X-Accept-Language: en-us, en In-Reply-To: Xref: g2news1.google.com comp.lang.ada:3788 Date: 2005-07-26T17:39:21+02:00 List-Id: Robert A Duff wrote: > If you use the default storage pool(s), the language standard does not > specify when heap memory is reclaimed. Most implementations do not > reclaim any such memory unless you explicitly call > Unchecked_Deallocation. OK. > If you put "for T'Storage_Size use 1_000_000;", then implementations > should reclaim all memory allocated for type T when the scope of T is > left. Nice. Why putting this arbitrary limit? And what does it mean, anyway - is the pool actually pre-allocated with this size when the for..use... statement is executed, or does it start empty and later "inflates" as necessary, but no bigger than the given limit? The difference is not only the observable memory consumption (even when not the whole pool is used), but also the possibility and timing of low-memory errors. > This is not > a very useful capability, because most access types need to be at > library level, so the memory won't be reclaimed until the whole program > is done. Indeed, not really useful. > You should look up user defined storage pools. You can say: > > for T'Storage_Pool use My_Pool; > > and then you can control when memory will be reclaimed. > You can reclaim all memory in My_Pool whenever you like -- but > beware dangling pointers. I have to beware them when using Unchecked_Deallocation as well. :) >>2. >> >>loop >> declare >> X : MyTypeRef; >> begin >> X := new MyType; >> end; >>end loop; >> >>What now? Is this any different from the memory management point of view? > > No. X.all will never be reclaimed (on most implementations). The only > difference here is that you're allocating only one object. Not really - there's a loop. I understand that the code above leaks memory, just like my first example. > Suppose we added a call P(X) inside the begin/end. And suppose P saves > X in a global variable. The implementation cannot deallocate the memory > X points to, because that would leave a dangling pointer in the global > variable. Right, but I was interested exactly in the case where there is just one reference and it is trivial for the compiler to prove that the object is not aliased. But without GC there's no difference anyway. >>4. >> >>Is it possible to associate some function with object allocated by new, >>which would be called at the time (or maybe after) the object is >>reclaimed? >>Yes, I'm asking about destructors or finalizers. > > Yes. Look up "controlled types". These allow you to associate a > Finalize operation with a type. For local variables, Finalize will be > automatically called when the procedure is left. Good - this, basically, should allow me to implement some form of local resource manager (in C++ this idiom is called RAII) that will deallocate the object (or any other resource) when the scope is left. > For heap objects, > Finalize will be called when you do Unchecked_Deallocation (or, if you > never do U_D, when the program is done). Fine. Is the order of calling finalizers well-defined for the latter case? Thank you for these explanations, -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/