Fix memory leaks

Change-Id: If9f7dc4a28d5e005959f0d4a0a2ed317b699f292
This commit is contained in:
Stephan Bergmann
2016-03-10 21:40:45 +01:00
parent 8e9a7cac42
commit eee4dd746e
3 changed files with 32 additions and 32 deletions

View File

@@ -255,6 +255,26 @@ struct JavaInfo
rtl::ByteSequence arVendorData; rtl::ByteSequence arVendorData;
}; };
namespace jfw {
struct JavaInfoGuard {
JavaInfoGuard(JavaInfoGuard &) = delete;
void operator =(JavaInfoGuard) = delete;
JavaInfoGuard(): info(nullptr) {}
~JavaInfoGuard() { delete info; }
void clear() {
delete info;
info = nullptr;
}
JavaInfo * info;
};
}
/** compares two <code>JavaInfo</code> objects for equality. /** compares two <code>JavaInfo</code> objects for equality.
<p>Two <code>JavaInfo</code> objects are said to be equal if the contained <p>Two <code>JavaInfo</code> objects are said to be equal if the contained

View File

@@ -64,8 +64,8 @@ SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
return -1; return -1;
} }
JavaInfo * pInfo = nullptr; jfw::JavaInfoGuard pInfo;
errcode = jfw_getSelectedJRE( & pInfo); errcode = jfw_getSelectedJRE(&pInfo.info);
if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS) if (errcode != JFW_E_NONE && errcode != JFW_E_INVALID_SETTINGS)
{ {
@@ -73,19 +73,19 @@ SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
return -1; return -1;
} }
if (pInfo == nullptr) if (pInfo.info == nullptr)
{ {
if (!findAndSelect(&pInfo)) if (!findAndSelect(&pInfo.info))
return -1; return -1;
} }
else else
{ {
//check if the JRE was not uninstalled //check if the JRE was not uninstalled
sal_Bool bExist = sal_False; sal_Bool bExist = sal_False;
errcode = jfw_existJRE(pInfo, &bExist); errcode = jfw_existJRE(pInfo.info, &bExist);
if (errcode == JFW_E_NONE) if (errcode == JFW_E_NONE)
{ {
if (!bExist && !findAndSelect(&pInfo)) if (!bExist && !findAndSelect(&pInfo.info))
return -1; return -1;
} }
else else
@@ -95,9 +95,8 @@ SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
} }
} }
OString sPaths = getLD_LIBRARY_PATH(pInfo->arVendorData); OString sPaths = getLD_LIBRARY_PATH(pInfo.info->arVendorData);
fprintf(stdout, "%s\n", sPaths.getStr()); fprintf(stdout, "%s\n", sPaths.getStr());
delete pInfo;
} }
catch (const std::exception&) catch (const std::exception&)
{ {

View File

@@ -657,23 +657,6 @@ JavaVirtualMachine::getSupportedServiceNames()
return serviceGetSupportedServiceNames(); return serviceGetSupportedServiceNames();
} }
namespace {
struct JavaInfoGuard: private boost::noncopyable {
JavaInfoGuard(): info(nullptr) {}
~JavaInfoGuard() { delete info; }
void clear() {
delete info;
info = nullptr;
}
JavaInfo * info;
};
}
css::uno::Any SAL_CALL css::uno::Any SAL_CALL
JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId) JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId)
throw (css::uno::RuntimeException, std::exception) throw (css::uno::RuntimeException, std::exception)
@@ -698,7 +681,7 @@ JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId)
if (aId != aProcessId) if (aId != aProcessId)
return css::uno::Any(); return css::uno::Any();
JavaInfoGuard info; jfw::JavaInfoGuard info;
while (!m_xVirtualMachine.is()) // retry until successful while (!m_xVirtualMachine.is()) // retry until successful
{ {
// This is the second attempt to create Java. m_bDontCreateJvm is // This is the second attempt to create Java. m_bDontCreateJvm is
@@ -816,14 +799,14 @@ JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId)
//we search another one. As long as there is a javaldx, we should //we search another one. As long as there is a javaldx, we should
//never come into this situation. javaldx checks always if the JRE //never come into this situation. javaldx checks always if the JRE
//still exist. //still exist.
JavaInfo * pJavaInfo = nullptr; jfw::JavaInfoGuard pJavaInfo;
if (JFW_E_NONE == jfw_getSelectedJRE(&pJavaInfo)) if (JFW_E_NONE == jfw_getSelectedJRE(&pJavaInfo.info))
{ {
sal_Bool bExist = sal_False; sal_Bool bExist = sal_False;
if (JFW_E_NONE == jfw_existJRE(pJavaInfo, &bExist)) if (JFW_E_NONE == jfw_existJRE(pJavaInfo.info, &bExist))
{ {
if (!bExist if (!bExist
&& ! (pJavaInfo->nRequirements & JFW_REQUIRE_NEEDRESTART)) && ! (pJavaInfo.info->nRequirements & JFW_REQUIRE_NEEDRESTART))
{ {
info.clear(); info.clear();
javaFrameworkError errFind = jfw_findAndSelectJRE( javaFrameworkError errFind = jfw_findAndSelectJRE(
@@ -836,8 +819,6 @@ JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId)
} }
} }
delete pJavaInfo;
//Error: %PRODUCTNAME requires a Java //Error: %PRODUCTNAME requires a Java
//runtime environment (JRE) to perform this task. The selected JRE //runtime environment (JRE) to perform this task. The selected JRE
//is defective. Please select another version or install a new JRE //is defective. Please select another version or install a new JRE