From 1e2737b3fa95e365cd66828bd923040b446d90d7 Mon Sep 17 00:00:00 2001 From: "pinkerton%netscape.com" Date: Wed, 31 May 2000 02:30:07 +0000 Subject: [PATCH] fix for bug 33250. fixes memory leaks by freeing strings in dtor. also switching to using nsCRT::strdup. r/a=brendan. git-svn-id: svn://10.0.0.236/trunk@71134 18797224-902f-48f8-a5cc-f745e15eee43 --- .../appshell/src/nsCommandLineService.cpp | 33 ++++++++++++++----- .../startup/src/nsCommandLineService.cpp | 33 ++++++++++++++----- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/mozilla/xpfe/appshell/src/nsCommandLineService.cpp b/mozilla/xpfe/appshell/src/nsCommandLineService.cpp index 196f673fe14..e2319a2e605 100644 --- a/mozilla/xpfe/appshell/src/nsCommandLineService.cpp +++ b/mozilla/xpfe/appshell/src/nsCommandLineService.cpp @@ -52,8 +52,8 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) //Insert the program name if (aArgv[0]) { - mArgList.AppendElement((void *)PL_strdup("-progname")); - mArgValueList.AppendElement((void *)PL_strdup(aArgv[0])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("-progname"))); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[0]))); mArgCount++; i++; } @@ -64,7 +64,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* An option that starts with -. May or many not * have a value after it. */ - mArgList.AppendElement((void *)PL_strdup(aArgv[i])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); //Increment the index to look ahead at the next option. i++; @@ -75,7 +75,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* All args have been parsed. Append a PR_TRUE for the * previous option in the mArgValueList */ - mArgValueList.AppendElement((void *)PL_strdup("1")); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("1"))); mArgCount++; break; } @@ -85,7 +85,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) * mArgValue array and retract the index so that this option * will get stored in the next iteration */ - mArgValueList.AppendElement((void *)PL_strdup("1")); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("1"))); mArgCount++; i--; continue; @@ -103,7 +103,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) // Append the url to the arrays //mArgList.AppendElement((void *)PL_strdup("-url")); - mArgValueList.AppendElement((void *) PL_strdup(aArgv[i])); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; continue; } @@ -111,7 +111,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* This is a value to the previous option. * Store it in the mArgValue array */ - mArgValueList.AppendElement((void *)PL_strdup(aArgv[i])); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; } } @@ -121,8 +121,8 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* This must be the URL at the end * Append the url to the arrays */ - mArgList.AppendElement((void *)PL_strdup("-url")); - mArgValueList.AppendElement((void *) PL_strdup(aArgv[i])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("-url"))); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; } else { @@ -237,6 +237,21 @@ nsCmdLineService::GetArgv(char *** aResult) nsCmdLineService::~nsCmdLineService() { + PRInt32 curr = mArgList.Count(); + while ( curr ) { + char* str = NS_REINTERPRET_CAST(char*, mArgList[curr-1]); + if ( str ) + nsAllocator::Free(str); + --curr; + } + + curr = mArgValueList.Count(); + while ( curr ) { + char* str = NS_REINTERPRET_CAST(char*, mArgValueList[curr-1]); + if ( str ) + nsAllocator::Free(str); + --curr; + } } diff --git a/mozilla/xpfe/components/startup/src/nsCommandLineService.cpp b/mozilla/xpfe/components/startup/src/nsCommandLineService.cpp index 196f673fe14..e2319a2e605 100644 --- a/mozilla/xpfe/components/startup/src/nsCommandLineService.cpp +++ b/mozilla/xpfe/components/startup/src/nsCommandLineService.cpp @@ -52,8 +52,8 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) //Insert the program name if (aArgv[0]) { - mArgList.AppendElement((void *)PL_strdup("-progname")); - mArgValueList.AppendElement((void *)PL_strdup(aArgv[0])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("-progname"))); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[0]))); mArgCount++; i++; } @@ -64,7 +64,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* An option that starts with -. May or many not * have a value after it. */ - mArgList.AppendElement((void *)PL_strdup(aArgv[i])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); //Increment the index to look ahead at the next option. i++; @@ -75,7 +75,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* All args have been parsed. Append a PR_TRUE for the * previous option in the mArgValueList */ - mArgValueList.AppendElement((void *)PL_strdup("1")); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("1"))); mArgCount++; break; } @@ -85,7 +85,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) * mArgValue array and retract the index so that this option * will get stored in the next iteration */ - mArgValueList.AppendElement((void *)PL_strdup("1")); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("1"))); mArgCount++; i--; continue; @@ -103,7 +103,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) // Append the url to the arrays //mArgList.AppendElement((void *)PL_strdup("-url")); - mArgValueList.AppendElement((void *) PL_strdup(aArgv[i])); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; continue; } @@ -111,7 +111,7 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* This is a value to the previous option. * Store it in the mArgValue array */ - mArgValueList.AppendElement((void *)PL_strdup(aArgv[i])); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; } } @@ -121,8 +121,8 @@ nsCmdLineService::Initialize(int aArgc, char ** aArgv) /* This must be the URL at the end * Append the url to the arrays */ - mArgList.AppendElement((void *)PL_strdup("-url")); - mArgValueList.AppendElement((void *) PL_strdup(aArgv[i])); + mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("-url"))); + mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[i]))); mArgCount++; } else { @@ -237,6 +237,21 @@ nsCmdLineService::GetArgv(char *** aResult) nsCmdLineService::~nsCmdLineService() { + PRInt32 curr = mArgList.Count(); + while ( curr ) { + char* str = NS_REINTERPRET_CAST(char*, mArgList[curr-1]); + if ( str ) + nsAllocator::Free(str); + --curr; + } + + curr = mArgValueList.Count(); + while ( curr ) { + char* str = NS_REINTERPRET_CAST(char*, mArgValueList[curr-1]); + if ( str ) + nsAllocator::Free(str); + --curr; + } }