From ripley at stats.ox.ac.uk  Thu Dec 30 17:27:59 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu Dec 30 17:28:04 2004
Subject: [Rd] optim/vmmin and R_alloc
In-Reply-To: <Pine.A41.4.61b.0412300757350.11816@homer11.u.washington.edu>
References: <20041230155513.GA22178@tal.stat.umu.se>
	<Pine.A41.4.61b.0412300757350.11816@homer11.u.washington.edu>
Message-ID: <Pine.LNX.4.61.0412301619400.14359@gannet.stats>

On Thu, 30 Dec 2004, Thomas Lumley wrote:

> On Thu, 30 Dec 2004, [iso-8859-1] Gran Brostrm wrote:
>> 
>> However, and that is my question/suggestion: Why not use Calloc+Free
>> instead of R_alloc in vmmin (and maybe in other places)? I made the
>> changes in 'optim.c' in the functions 'vmmin' and 'Lmatrix'. Then I rebuilt
>> and reinstalled R-2.0.1. The result was great! My function ran very fast 
>> and
>> without any memory problems.
>> 
>> So why not? Or will this change create other problems?
>
> Memory will leak permanently if the functions are interrupted or an error 
> occurs.  This may be less serious now than it was in the past, since R can be 
> interrupted in fewer places.

An error in the objective function would also cause a leak. Goran *should* 
be managing the heap himself by vmax[gs]et, as discussed in Writing R 
Extensions.

So why not read the manual and use the documented mechanism?  I do
often wonder if the developers spend more time writing these 
manuals than the users collectively spend reading them.

To answer the follow-up, some code can be demonstrated not to leak as it 
cannot be interrupted, and some wants memory preserved across .C calls.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

[Rd] optim/vmmin and R_alloc
Prof Brian Ripley