r.in.sunrast / memory leaks

Michael Shapiro shapiro at zorro.cecer.army.mil
Fri Sep 18 16:42:18 EDT 1992


r.in.sunrast has been upgraded for 4.1  to  (1)  deal  with  this
requirement  for the correct number of cols and (2) to accept bit
images as well.  I will try to get the new version on moon soon.

As for memory leaks, GRASS is FULL of them. Even if this  program
freed the memory allocated by G_malloc() there are many places in
GRASS where memory leaks occur. For  example,  a  subroutine  may
return a pointer to a character string. This pointer may point to
allocated  memory  or  it  may  point  to  static   memory.   The
documentation  doesn't  say  this.   So  programmers  don't  have
complete control on freeing all memory used by  GRASS  except  to
rely  on  program  exit to clean up. Un fortunately it would be a
big effort to get rid of many of these leaks so it probably won't
happen.

Does anyone know of a Unix system that doesn't  return  allocated
memory to the system? (This isn't meant to be a cynical question).

|
|
|Helpful suggestions for the fix to r.in.sunrast have been offered
|by :
|
|Jim Hinthorne,
|Director GIS Lab
|Central Wash. Univ.
|
|He pointed out that :
|
|Two more changes are needed to make this program bullit-pruff.
|
|1.  The parameter passed to G_malloc should be ncol_fix, not ncols,
|because this is the size of the record to read later.
|
|2.  Before the final "return 1;" there should be "free (raster);" so
|that the space allocated by G_malloc will be freed up.  Some systems
|are good about doing this automatically when the program exits, but
|some aren't so friendly and the programmer should take responsibility
|for freeing allocated memory.
|
|
|So, the new fix to r.in.sunrast would be :
|
|
|...........................................................
|
|cd src.alpha/raster/r.in.sunrast/cmd
|
|and edit main.c
|
|replace the function rasttocell with :
|
|rasttocell (rast_fd, cell_fd, nrows, ncols)
|{
|    unsigned char *raster;
|    CELL *cell;
|    int row, col, ncol_fix ;
|
|    /* check if row is even or odd */
|    ncol_fix = ncols;
|    if ( ncols % 2 )
|           ncol_fix += 1 ;
|
|    cell = G_allocate_cell_buf();
|
|    /* JH fix 1 */
|    raster = (unsigned char *) G_malloc(ncol_fix);
|
|
|    if (verbose)
|        fprintf (stderr, "complete ... ");
|    for (row = 0; row < nrows; row++)
|    {
|        if (verbose)
|            G_percent (row, nrows, 2);
|        if (read (rast_fd, raster, ncol_fix) != ncol_fix)
|            return -1;
|        for (col = 0; col < ncols; col++)
|            cell[col] = (CELL) raster[col];
|        if (G_put_map_row (cell_fd, cell) < 0)
|            exit(1);
|    }
|
|    
|    if (verbose)
|        G_percent (row, nrows, 2);
|
|    /* JH Fix 2 */
|    free (raster);
|    return 1;
|}
|
|and recompile
|
|...........................................................
|
|Thanks, Jim 
|
|Bob Courtney
|Atlantic Geoscience Centre
|
|


-----------------------------------------------------------------
Michael Shapiro                        U.S. Army CERL                  
email:   shapiro at zorro.cecer.army.mil  Environmental Division          
phone:   (217) 373-7277                P.O. Box 9005                   
fax:     (217) 373-7222                Champaign, Ill. 61826-9005
-----------------------------------------------------------------




More information about the grass-user mailing list