Not seeing the point of converting too and from the C++ string type.
Why not just use char* ?
On Sun, Mar 27, 2011 at 12:47 AM, Russell Bryant
<russell(a)russellbryant.net> wrote:
1) Resolve some memory leaks in places where strdup() was being
used but was not cleaned up. These strings were converted to
std::string.
2) Remove an object from MatahariAgent::init() that was unused.
Signed-off-by: Russell Bryant <russell(a)russellbryant.net>
---
src/lib/mh_agent.cpp | 83 ++++++++++++++++++++++++++++++++------------------
1 files changed, 53 insertions(+), 30 deletions(-)
diff --git a/src/lib/mh_agent.cpp b/src/lib/mh_agent.cpp
index 3f72d0f..e103dc1 100644
--- a/src/lib/mh_agent.cpp
+++ b/src/lib/mh_agent.cpp
@@ -140,13 +140,11 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
#endif
bool gssapi = false;
- char *servername = strdup(MATAHARI_BROKER);
- char *username = NULL;
- char *password = NULL;
- char *service = NULL;
- int serverport = MATAHARI_PORT;
-
- qpid::management::ConnectionSettings settings;
+ string servername;
+ string username;
+ string password;
+ string service;
+ int serverport = MATAHARI_PORT;
/* Set up basic logging */
mh_log_init(proc_name, LOG_INFO, FALSE);
@@ -168,7 +166,13 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\services\\Matahari",
L"Broker",
- &servername);
+ &value);
+
+ if(value) {
+ servername = value;
+ free(value);
+ value = NULL;
+ }
RegistryRead (
HKEY_LOCAL_MACHINE,
@@ -186,19 +190,38 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\services\\Matahari",
L"Service",
- &service);
+ &value);
+
+ if(value) {
+ service = value;
+ free(value);
+ value = NULL;
+ }
RegistryRead (
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\services\\Matahari",
L"User",
- &username);
+ &value);
+
+ if(value) {
+ username = value;
+ free(value);
+ value = NULL;
+ }
+
RegistryRead (
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\services\\Matahari",
L"Password",
- &password);
-
+ &value);
+
+ if(value) {
+ password = value;
+ free(value);
+ value = NULL;
+ }
+
#else
// Get args
@@ -218,7 +241,7 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
break;
case 's':
if (optarg) {
- service = strdup(optarg);
+ service = optarg;
} else {
print_usage(proc_name);
exit(1);
@@ -226,7 +249,7 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
break;
case 'u':
if (optarg) {
- username = strdup(optarg);
+ username = optarg;
} else {
print_usage(proc_name);
exit(1);
@@ -234,7 +257,7 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
break;
case 'P':
if (optarg) {
- password = strdup(optarg);
+ password = optarg;
} else {
print_usage(proc_name);
exit(1);
@@ -253,7 +276,7 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
break;
case 'b':
if (optarg) {
- servername = strdup(optarg);
+ servername = optarg;
} else {
print_usage(proc_name);
exit(1);
@@ -275,29 +298,27 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
}
#endif
+ if (servername.length() == 0) {
+ servername = MATAHARI_BROKER;
+ }
+
/* Re-initialize logging now that we've completed option processing */
mh_log_init(proc_name, mh_log_level, mh_log_level > LOG_INFO);
// Set up the cleanup handler for sigint
signal(SIGINT, shutdown);
- // Connect to the broker
- settings.host = servername;
- settings.port = serverport;
-
- mh_info("Connecting to Qpid broker at %s on port %d", servername,
serverport);
-
// Create a v2 API options map.
qpid::types::Variant::Map options;
options["reconnect"] = bool(true);
- if (username) {
- options["username"] = username;
+ if (!username.empty()) {
+ options["username"] = username.c_str();
}
- if (password) {
- options["password"] = password;
+ if (!password.empty()) {
+ options["password"] = password.c_str();
}
- if (service) {
- options["sasl-service"] = service;
+ if (!service.empty()) {
+ options["sasl-service"] = service.c_str();
}
if (gssapi) {
options["sasl-mechanism"] = "GSSAPI";
@@ -306,6 +327,8 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
std::stringstream url;
url << servername << ":" << serverport ;
+ mh_info("Connecting to Qpid broker at %s", url.str().c_str());
+
_amqp_connection = qpid::messaging::Connection(url.str(), options);
_amqp_connection.open();
@@ -318,8 +341,8 @@ MatahariAgent::init(int argc, char **argv, const char* proc_name)
/* Do any setup required by our agent */
if(this->setup(_agent_session) < 0) {
- mh_err("Failed to set up broker connection to %s on %d for %s\n",
- servername, serverport, proc_name);
+ mh_err("Failed to set up broker connection to %s for %s\n",
+ url.str().c_str(), proc_name);
return -1;
}
--
1.7.4
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari