Frédéric Bohé
2011-Jan-19 07:47 UTC
[Nut-upsdev] [nut-commits] svn commit r2845 - branches/windows_port/common
On mar., 2011-01-18 at 19:52 +0000, Arjen de Korte wrote:> Author: adkorte-guest > Date: Tue Jan 18 19:52:02 2011 > New Revision: 2845 > URL: http://trac.networkupstools.org/projects/nut/changeset/2845 > > Log: > Prefer to use static variables locally over global variables (to prevent namespace conflicts) > > Modified: > branches/windows_port/common/common.c > > Modified: branches/windows_port/common/common.c > =============================================================================> --- branches/windows_port/common/common.c Tue Jan 18 15:40:58 2011 (r2844) > +++ branches/windows_port/common/common.c Tue Jan 18 19:52:02 2011 (r2845) > @@ -41,11 +41,6 @@ > int nut_log_level = 0; > static int upslog_flags = UPSLOG_STDERR; > > -#ifdef WIN32 > -const char * relative_conf_path = NULL; > -const char * relative_state_path = NULL; > -#endif > - > static void xbit_set(int *val, int flag) > { > *val |= flag; > @@ -440,51 +435,25 @@ > > > /* Return the default path for the directory containing configuration files */ > -const char * confpath(void) > +const char * confpath(void) > { > - const char * path; > - > - if ((path = getenv("NUT_CONFPATH")) == NULL) { > #ifndef WIN32 > - path = CONFPATH; > + const char *path = getenv("NUT_CONFPATH"); > #else > - if( relative_conf_path == NULL ) { > - path = getfullpath(PATH_ETC); > - if( path == NULL ) { > - path = CONFPATH; > - } > - } > - else { > - path = relative_conf_path; > - } > + static const char *path = getfullpath(PATH_ETC);getfullpath is allocating memory with xstrdup. Doesn't this line produce a memory leak ?> #endif > - } > - > - return path; > + return (path != NULL) ? path : CONFPATH; > } > > /* Return the default path for the directory containing state files */ > -const char * dflt_statepath(void) > +const char * dflt_statepath(void) > { > - const char * path; > - > - if ((path = getenv("NUT_STATEPATH")) == NULL) { > #ifndef WIN32 > - path = STATEPATH; > + const char *path = getenv("NUT_STATEPATH"); > #else > - if( relative_state_path == NULL ) { > - path = getfullpath(PATH_VAR_RUN); > - if( path == NULL ) { > - path = STATEPATH; > - } > - } > - else { > - path = relative_state_path; > - } > + static const char *path = getfullpath(PATH_VAR_RUN); > #endif > - } > - > - return path; > + return (path != NULL) ? path : STATEPATH; > } > > /* Return the alternate path for pid files */ > > _______________________________________________ > nut-commits mailing list > nut-commits at lists.alioth.debian.org > http://lists.alioth.debian.org/mailman/listinfo/nut-commits--------------------------------------------------------------------------
Arjen de Korte
2011-Jan-19 08:12 UTC
[Nut-upsdev] [nut-commits] svn commit r2845 - branches/windows_port/common
Citeren Fr?d?ric Boh? <fredericbohe op eaton.com>:>> + static const char *path = getfullpath(PATH_ETC); > > getfullpath is allocating memory with xstrdup. Doesn't this line produce > a memory leak ?Nope. Whenever you do an assignment to a static variable, this is only done once. So the getfullpath() function will be called the first time you enter this function and afterwards, the result of this call will be used. Unlike an ordinary automatic variable, static variables defined in functions are preserved between calls. We usually prefer static local variables over global ones, because it prevents namespace conflicts and also prevents other function from messing with the variables. Best regards, Arjen -- Please keep list traffic on the list (off-list replies will be rejected)
Apparently Analagous Threads
- [nut-commits] svn commit r2839 - branches/windows_port/scripts/Windows
- [nut-commits] svn commit r3554 - branches/windows_port/common
- Missing files on windows_port branch
- [nut-commits] svn commit r2853 - in branches/windows_port: drivers include
- Merging windows_port branch