Memory audit of code in src directory. Found and fixed some leaks.

Declaring this directory memory clean.

M src/Pluglet.cpp
M src/Pluglet.h

- use do_GetService instead of servman->GetServiceByContractID

- do not keep a reference to plugletEngine as an ivar

M src/PlugletFactory.cpp
M src/PlugletFactory.h

- use nsnull instead of NULL

- do not keep a reference to plugletEngine as an ivar

- use PL_strdup and PL_strfree to duplicate and free strings

M src/PlugletsDir.cpp

- use PL_strdup and PL_strfree to duplicate and free strings
This commit is contained in:
edburns%acm.org 2006-11-09 20:10:17 +00:00
Родитель fe7743592b
Коммит 3bafb8cb42
5 изменённых файлов: 68 добавлений и 49 удалений

Просмотреть файл

@ -42,13 +42,10 @@ static NS_DEFINE_IID(kIPluginInstanceIID, NS_IPLUGININSTANCE_IID);
NS_IMPL_ISUPPORTS1(Pluglet,nsIPluginInstance); NS_IMPL_ISUPPORTS1(Pluglet,nsIPluginInstance);
Pluglet::Pluglet(jobject object) : plugletEngine(nsnull), peer(nsnull) { Pluglet::Pluglet(jobject object) : peer(nsnull) {
nsIServiceManager *servman = nsnull;
NS_GetServiceManager(&servman);
nsresult rv; nsresult rv;
rv = servman->GetServiceByContractID(PLUGLETENGINE_ContractID, nsCOMPtr<iPlugletEngine> plugletEngine =
NS_GET_IID(iPlugletEngine), do_GetService(PLUGLETENGINE_ContractID, &rv);
getter_AddRefs(plugletEngine));
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
("Pluglet::Pluglet: Cannot access iPlugletEngine service\n")); ("Pluglet::Pluglet: Cannot access iPlugletEngine service\n"));
@ -74,7 +71,8 @@ Pluglet::~Pluglet() {
Registry::Remove(jthis); Registry::Remove(jthis);
JNIEnv *jniEnv = nsnull; JNIEnv *jniEnv = nsnull;
nsresult rv; nsresult rv;
plugletEngine = do_GetService(PLUGLETENGINE_ContractID, &rv); nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
if (plugletEngine) { if (plugletEngine) {
rv = plugletEngine->GetJNIEnv(&jniEnv); rv = plugletEngine->GetJNIEnv(&jniEnv);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -103,6 +101,9 @@ NS_METHOD Pluglet::Initialize(nsIPluginInstancePeer* _peer) {
("Pluglet::Initialize\n")); ("Pluglet::Initialize\n"));
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -175,6 +176,8 @@ NS_METHOD Pluglet::Start(void) {
("Pluglet::Start\n")); ("Pluglet::Start\n"));
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
@ -193,6 +196,8 @@ NS_METHOD Pluglet::Stop(void) {
("Pluglet::Stop\n")); ("Pluglet::Stop\n"));
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
@ -211,6 +216,8 @@ NS_METHOD Pluglet::Destroy(void) {
("Pluglet::Destroy\n")); ("Pluglet::Destroy\n"));
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
@ -234,6 +241,8 @@ NS_METHOD Pluglet::NewStream(nsIPluginStreamListener** listener) {
} }
nsresult rv; nsresult rv;
JNIEnv *env; JNIEnv *env;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
@ -265,6 +274,8 @@ NS_METHOD Pluglet::SetWindow(nsPluginWindow* window) {
if (view->SetWindow(window) == PR_TRUE) { if (view->SetWindow(window) == PR_TRUE) {
nsresult rv; nsresult rv;
JNIEnv *env; JNIEnv *env;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,

Просмотреть файл

@ -54,6 +54,5 @@ class Pluglet : public nsIPluginInstance {
static jmethodID getValueMID; static jmethodID getValueMID;
nsCOMPtr<nsIPluginInstancePeer> peer; nsCOMPtr<nsIPluginInstancePeer> peer;
PlugletView *view; PlugletView *view;
nsCOMPtr<iPlugletEngine> plugletEngine;
}; };
#endif /* __Pluglet_h__ */ #endif /* __Pluglet_h__ */

Просмотреть файл

@ -29,9 +29,9 @@
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
jmethodID PlugletFactory::createPlugletMID = NULL; jmethodID PlugletFactory::createPlugletMID = nsnull;
jmethodID PlugletFactory::initializeMID = NULL; jmethodID PlugletFactory::initializeMID = nsnull;
jmethodID PlugletFactory::shutdownMID = NULL; jmethodID PlugletFactory::shutdownMID = nsnull;
nsresult PlugletFactory::CreatePluginInstance(const char* aPluginMIMEType, void **aResult) { nsresult PlugletFactory::CreatePluginInstance(const char* aPluginMIMEType, void **aResult) {
@ -44,6 +44,8 @@ nsresult PlugletFactory::CreatePluginInstance(const char* aPluginMIMEType, void
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv = NS_ERROR_FAILURE; nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -71,6 +73,8 @@ nsresult PlugletFactory::Initialize(void) {
("PlugletFactory::Initialize\n")); ("PlugletFactory::Initialize\n"));
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -130,6 +134,8 @@ nsresult PlugletFactory::Shutdown(void) {
} }
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -155,31 +161,25 @@ nsresult PlugletFactory::GetMIMEDescription(const char* *result) {
return (*result) ? NS_OK : NS_ERROR_FAILURE; return (*result) ? NS_OK : NS_ERROR_FAILURE;
} }
PlugletFactory::PlugletFactory(const char *mimeDescription, const char *path) : plugletEngine(nsnull) { PlugletFactory::PlugletFactory(const char *inMimeDescription, const char *inPath) : jthis(nsnull), path(PL_strdup(inPath)), mimeDescription(PL_strdup(inMimeDescription))
jthis = NULL; {
this->path = new char[strlen(path)+1];
strcpy(this->path,path);
this->mimeDescription = new char[strlen(mimeDescription)+1];
strcpy(this->mimeDescription,mimeDescription);
nsresult rv = NS_ERROR_FAILURE;
plugletEngine = do_GetService(PLUGLETENGINE_ContractID, &rv);
if (NS_FAILED(rv)) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
("Pluglet::PlugletFactory: Cannot access iPlugletEngine service\n"));
return;
}
} }
PlugletFactory::~PlugletFactory(void) { PlugletFactory::~PlugletFactory(void) {
if (path != NULL) {
delete[] path; if (this->path) {
PL_strfree(this->path);
this->path = nsnull;
} }
if (mimeDescription != NULL) { if (this->mimeDescription) {
delete[] mimeDescription; PL_strfree(this->mimeDescription);
this->mimeDescription = nsnull;
} }
JNIEnv *env = nsnull; JNIEnv *env = nsnull;
nsresult rv; nsresult rv;
nsCOMPtr<iPlugletEngine> plugletEngine =
do_GetService(PLUGLETENGINE_ContractID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = plugletEngine->GetJNIEnv(&env); rv = plugletEngine->GetJNIEnv(&env);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -188,16 +188,17 @@ PlugletFactory::~PlugletFactory(void) {
return; return;
} }
if (env != NULL) { if (env != nsnull) {
env->DeleteGlobalRef(jthis); env->DeleteGlobalRef(jthis);
} }
}
} }
PlugletFactory * PlugletFactory::Load(const char * path) { PlugletFactory * PlugletFactory::Load(const char * path) {
PR_LOG(PlugletLog::log, PR_LOG_DEBUG, PR_LOG(PlugletLog::log, PR_LOG_DEBUG,
("PlugletFactory::Load\n")); ("PlugletFactory::Load\n"));
char * mime = PlugletLoader::GetMIMEDescription(path); char * mime = PlugletLoader::GetMIMEDescription(path);
PlugletFactory * result = NULL; PlugletFactory * result = nsnull;
if (mime) { if (mime) {
result = new PlugletFactory(mime,path); result = new PlugletFactory(mime,path);
//delete[] mime; //nb we have a strange exception here //delete[] mime; //nb we have a strange exception here
@ -217,16 +218,16 @@ int PlugletFactory::Compare(const char *mimeType) {
if (!mimeType) { if (!mimeType) {
return 0; return 0;
} }
char * terminator = strchr(mimeDescription,':'); // terminator have to be not equal to NULL; char * terminator = strchr(mimeDescription,':'); // terminator have to be not equal to nsnull;
char *p1 = mimeDescription; char *p1 = mimeDescription;
char *p2 = strchr(p1,';'); char *p2 = strchr(p1,';');
while ( p1 != NULL && p1 < terminator ) { while ( p1 != nsnull && p1 < terminator ) {
size_t n = sizeof(char) * ( ( (p2 == NULL || p2 > terminator) ? terminator : p2) - p1 ); size_t n = sizeof(char) * ( ( (p2 == nsnull || p2 > terminator) ? terminator : p2) - p1 );
if (PL_strncasecmp(p1,mimeType,n) == 0) { if (PL_strncasecmp(p1,mimeType,n) == 0) {
return 1; return 1;
} }
p1 = p2 ; p1 = p2 ;
if (p2 != NULL) { if (p2 != nsnull) {
p2 = strchr(++p2,';'); p2 = strchr(++p2,';');
p1++; p1++;
} }

Просмотреть файл

@ -43,7 +43,6 @@ class PlugletFactory {
char *mimeDescription; char *mimeDescription;
char *path; char *path;
PlugletFactory(const char *mimeDescription,const char * path); PlugletFactory(const char *mimeDescription,const char * path);
nsCOMPtr<iPlugletEngine> plugletEngine;
}; };
#endif /* __PlugletFactory_h__ */ #endif /* __PlugletFactory_h__ */

Просмотреть файл

@ -28,13 +28,15 @@
static PRIntn DeleteEntryEnumerator(PLHashEntry *he, PRIntn i, void *arg) static PRIntn DeleteEntryEnumerator(PLHashEntry *he, PRIntn i, void *arg)
{ {
PLHashTable *hash = (PLHashTable *) arg; PLHashTable *hash = (PLHashTable *) arg;
PL_HashTableRemove(hash, he->key);
void *key = (void *) he->key; char *key = (char *) he->key;
nsMemory::Free(key);
PL_strfree(key);
PlugletFactory *toDelete = (PlugletFactory *) he->value; PlugletFactory *toDelete = (PlugletFactory *) he->value;
delete toDelete; delete toDelete;
PL_HashTableRemove(hash, he->key);
return 0; return 0;
} }
@ -102,14 +104,21 @@ nsresult PlugletsDir::LoadPluglets() {
mimeType = nsnull; mimeType = nsnull;
rv = plugletFactory->GetMIMEDescription(&mimeType); rv = plugletFactory->GetMIMEDescription(&mimeType);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
const char *key = PL_strdup(mimeType);
printf("debug: edburns: key: %s address: %p\n",
key, key);
PLHashEntry *entry = PLHashEntry *entry =
PL_HashTableAdd(mMimeTypeToPlugletFacoryHash, PL_HashTableAdd(mMimeTypeToPlugletFacoryHash,
(const void *) PL_strdup(mimeType), (const void *) key,
plugletFactory); plugletFactory);
rv = (nsnull != entry) ? NS_OK : NS_ERROR_FAILURE; rv = (nsnull != entry) ? NS_OK : NS_ERROR_FAILURE;
} }
} }
} }
if (path) {
PL_strfree(path);
}
} }
return rv; return rv;
} }