On Thu, Jul 09, 2009 at 03:51:27PM +0200, Werner Fink wrote:
> Hi,
> 
> I've just to report a memory leak caused by a shell function which
> uses an echo as last line in the shell function.
> 
> -----------------------------------------------------------------------
> #!/usr/bin/ksh
> 
> PATH=/bin:/usr/bin:/usr/sbin:/sbin
> 
> # Description:  Get the interval to sample information
> # Output:       interval seconds
> getSampleInterval()
> {
>     # Example for this arithmetic, suppose the next time is 45 second
>     # If current second is in [35, 40], such as 38, 
>     #   the interval should be 10 - 38 % 10 + 5 = 7
>     # If current second is in [41, 45], such as 42,
>     #   the interval should be 10 - 42 % 10 - 5 = 3
>     # If the current second is 45, the interval should be 10 - 45 % 10 - 5 = 0
> 
>     typeset -ilu _currSec=$(date +%S)
>     typeset -ilu _interval
> 
>     ((_interval=10-(_currSec%10)))
> 
>     if ((_interval >= 5))
>     then 
>       ((_interval=_interval-5))
>     else
>       ((_interval=_interval+5))
>     fi
> 
>     echo $_interval    
> }
> 
> typeset -ilu times=0
> typeset -ilu interval
> 
> while [ 1 -eq 1 ]
> do
>     # First, get the interval to check
>     interval=$(getSampleInterval)
>     sleep $interval
> 
>     ((times++))
> 
>     state=$(egrep '^VmData|^VmRSS|^VmSize' </proc/$$/status)
>     [ "$state" != "$oldstate" ] && echo $state
>     oldstate="$state"
> done
> -----------------------------------------------------------------------

Found the following three problems:

in src/cmd/ksh93/sh/xec.c in function sh_funct() in the first line

                nv_putval(SH_FUNNAMENOD, nv_name(np),NV_NOFREE);

the allocated Namfun_t of the builtin node SH_FUNNAMENOD is set
but never freed in src/cmd/ksh93/sh/subshell.c in function nv_restore().

The same for src/cmd/ksh93/bltins/misc.c in function b_dot_cmd()
in the first line

        nv_putval(SH_PATHNAMENOD, shp->st.filename ,NV_NOFREE);

the allocated Namfun_t of the builtin node SH_PATHNAMENOD is set
but never freed in src/cmd/ksh93/sh/subshell.c in function nv_restore().

At last but not least in src/cmd/ksh93/sh/xec.c in function init_level()
the struct Level is allocated as Namfun_t of the builtin node
SH_LEVELNOD but also never freed in nv_restore().

In src/cmd/ksh93/sh/subshell.c in function nv_restore() the
allocated Namfun_t will be overwritten in the line

                mp->nvfun = np->nvfun;

moreover it seems that at least for the nodes SH_FUNNAMENOD and
SH_PATHNAMENOD the cached values will be cloned by allocation
further memory for the already allocated Namfun_t for every
cycle of the loop.

How this can be avoided?

       Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
_______________________________________________
ast-developers mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to