diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2017-04-13 15:19:17 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2017-04-13 17:32:43 +0200 |
commit | 626dec44e33727a56353efb7f4eee83e93bc2f3d (patch) | |
tree | 629bb290fee1689d1484d7c430b475da7082994e | |
parent | 6925007c2a86d45a8d71f08ef46e56bb3eda21e3 (diff) |
Use std::unique_ptr<JavaInfo> in jfw_plugin_getJavaInfosFromPath
...thereby fixing a memory leak
Change-Id: I1aa91eeb407987abcdaa5221f4abd447f881c5d2
-rw-r--r-- | jvmfwk/inc/vendorplugin.hxx | 8 | ||||
-rw-r--r-- | jvmfwk/plugins/sunmajor/pluginlib/sunjavaplugin.cxx | 10 | ||||
-rw-r--r-- | jvmfwk/source/framework.cxx | 37 |
3 files changed, 24 insertions, 31 deletions
diff --git a/jvmfwk/inc/vendorplugin.hxx b/jvmfwk/inc/vendorplugin.hxx index d456fe74a59c..875185551688 100644 --- a/jvmfwk/inc/vendorplugin.hxx +++ b/jvmfwk/inc/vendorplugin.hxx @@ -25,6 +25,8 @@ #include <rtl/ref.hxx> #include <rtl/ustring.h> #include "jni.h" + +#include <memory> #include <vector> #include <utility> #include "elements.hxx" @@ -204,10 +206,6 @@ javaPluginError jfw_plugin_getJavaInfoFromJavaHome( The JavaInfo structures returned in <code>vecJavaInfosFromPath</code> should be ordered according to their occurrence in the PATH. The one that is the first one on the PATH is also the first element in the vector.</p> - <p> - The function allocates memory for all the JavaInfo objects returned - in <code>vecJavaInfosFromPath</code>. The caller must delete each JavaInfo object. - </p> @param vecVendorInfos [in] vector specifying the vendor and version requirements that the JRE must fulfill. The vector contains pairs of vendors and the respective version requirements @@ -230,7 +228,7 @@ javaPluginError jfw_plugin_getJavaInfoFromJavaHome( javaPluginError jfw_plugin_getJavaInfosFromPath( std::vector<std::pair<OUString, jfw::VersionInfo>> const& vecVendorInfos, - std::vector<JavaInfo*> & vecJavaInfosFromPath, + std::vector<std::unique_ptr<JavaInfo>> & vecJavaInfosFromPath, std::vector<rtl::Reference<jfw_plugin::VendorBase>> & infos); /** starts a Java Virtual Machine. diff --git a/jvmfwk/plugins/sunmajor/pluginlib/sunjavaplugin.cxx b/jvmfwk/plugins/sunmajor/pluginlib/sunjavaplugin.cxx index 7ade439ed694..da7f6218d0d3 100644 --- a/jvmfwk/plugins/sunmajor/pluginlib/sunjavaplugin.cxx +++ b/jvmfwk/plugins/sunmajor/pluginlib/sunjavaplugin.cxx @@ -32,6 +32,7 @@ #include <cassert> #include <memory> +#include <utility> #include <vector> #include "config_options.h" @@ -424,14 +425,14 @@ javaPluginError jfw_plugin_getJavaInfoFromJavaHome( javaPluginError jfw_plugin_getJavaInfosFromPath( std::vector<std::pair<OUString, jfw::VersionInfo>> const& vecVendorInfos, - std::vector<JavaInfo*> & javaInfosFromPath, + std::vector<std::unique_ptr<JavaInfo>> & javaInfosFromPath, std::vector<rtl::Reference<jfw_plugin::VendorBase>> & infos) { // find JREs from PATH vector<rtl::Reference<VendorBase>> vecInfosFromPath; addJavaInfosFromPath(infos, vecInfosFromPath); - vector<JavaInfo*> vecVerifiedInfos; + vector<std::unique_ptr<JavaInfo>> vecVerifiedInfos; // copy infos of JREs that meet version requirements to vecVerifiedInfos typedef vector<rtl::Reference<VendorBase> >::iterator it; @@ -455,7 +456,8 @@ javaPluginError jfw_plugin_getJavaInfosFromPath( if (errorcode == javaPluginError::NONE) { - vecVerifiedInfos.push_back(createJavaInfo(currentInfo)); + vecVerifiedInfos.push_back( + std::unique_ptr<JavaInfo>(createJavaInfo(currentInfo))); } } } @@ -464,7 +466,7 @@ javaPluginError jfw_plugin_getJavaInfosFromPath( if (vecVerifiedInfos.empty()) return javaPluginError::NoJre; - javaInfosFromPath = vecVerifiedInfos; + javaInfosFromPath = std::move(vecVerifiedInfos); return javaPluginError::NONE; } diff --git a/jvmfwk/source/framework.cxx b/jvmfwk/source/framework.cxx index 63d5733f445a..0dd643d92d45 100644 --- a/jvmfwk/source/framework.cxx +++ b/jvmfwk/source/framework.cxx @@ -384,35 +384,28 @@ javaFrameworkError jfw_findAndSelectJRE(std::unique_ptr<JavaInfo> *pInfo) // query PATH for Java installations if (!bInfoFound) { - std::vector<JavaInfo*> vecJavaInfosFromPath; + std::vector<std::unique_ptr<JavaInfo>> vecJavaInfosFromPath; if (jfw_plugin_getJavaInfosFromPath( versionInfos, vecJavaInfosFromPath, infos) == javaPluginError::NONE) { - std::vector<JavaInfo*>::const_iterator it = vecJavaInfosFromPath.begin(); - while(it != vecJavaInfosFromPath.end() && !bInfoFound) + for (auto & pJInfo: vecJavaInfosFromPath) { - JavaInfo* pJInfo = *it; - if (pJInfo != nullptr) + // if the current Java installation implements all required features: use it + if ((pJInfo->nFeatures & nFeatureFlags) == nFeatureFlags) { - // if the current Java installation implements all required features: use it - if ((pJInfo->nFeatures & nFeatureFlags) == nFeatureFlags) - { - aCurrentInfo.reset(pJInfo); - bInfoFound = true; - } - else if (!aCurrentInfo) - { - // current Java installation does not provide all features - // but no Java installation has been detected before - // -> remember the current one until one is found - // that provides all features - aCurrentInfo.reset(pJInfo); - } - else - delete pJInfo; + aCurrentInfo = std::move(pJInfo); + bInfoFound = true; + break; + } + else if (!aCurrentInfo) + { + // current Java installation does not provide all features + // but no Java installation has been detected before + // -> remember the current one until one is found + // that provides all features + aCurrentInfo = std::move(pJInfo); } - ++it; } } } |