I have been working on an update to my application to make it more dynamic.
One of the features that I am working towards is to allow the application to
use threading. Now at this point I am not to that point yet, I am mainly
just working on the overall structure and cleaning up code.
In my efforts to enable my application to implement threading. I have setup
a file that sets/returns different variables that the threads will need. The
current issue that I am having is that when I go to set the value of these
variables from the data returned from my query I get a segfault that I
should not be getting. I have narrowed it down to exactly where the segfault
occurs. I am just not sure why.
So here comes the code. The segfault occurs in DataParse.cpp, where I have
the comments to the right of the code in upper case. Any help with this is
greatly appreciated.
*main (retsCom.cpp)*
#include <iostream>
#include "config/DoConfig.h"
#include "dbactions/DbConnect.h"
#include "parsing/DataParse.h"
using namespace std;
int main(int argc, char* argv[]) {
DataParse dparse;
DoConfig mconfig;
DbConnect dbConn;
if(mconfig.setConfig()){
// query data base for all instances active or not
mysqlpp::StoreQueryResult result = dbConn.db_query("select inst_uuid
from rcom_instances");
for (size_t i = 0; i < result.num_rows(); ++i) {
// init instance variables
dparse.rets_init(dbConn.mysqlStringToString(result[i][0]));
}
}
return 0;
}
*
DataParse.cpp*
#include "DataParse.h"
DataParse::DataParse(){
}
int DataParse::rets_init(std::string instanceUUID){
DbConnect dbConn;
// create query string
std::string sqlquery = "SELECT "
"rcom_instances.username as instUser, "
"rcom_instances.userpass as instPass, "
"rcom_instances.login_uri as instURI, "
"rcom_client.username as cliUser, "
"rcom_client.userpass as cliPass, "
"rcom_client.hostname as cliHost, "
"rcom_client.datasource as cliSource "
"FROM "
"rcom_instances "
"Inner Join rcom_client ON
rcom_instances.inst_uuid = rcom_client.inst_uuid "
"WHERE "
"rcom_instances.inst_uuid = '" +
instanceUUID + "'";
// Query db server for rets info
mysqlpp::StoreQueryResult result =
dbConn.db_query(sqlquery.c_str());
std::cout << result.num_fields() << std::endl; // THIS OUTPUTS THE
CORRECT NUMBER OF FIELDS
iActions.setClientHostname("Test Test"); // THIS DOES WORK
CORRECTLY
std::cout << iActions.getClientHostname() << std::endl; // THIS
OUTPUTS CORRECTLY
iActions.setClientDatasource(dbConn.mysqlStringToString(result[0][0])); //
THIS IS WHERE I GET THE SEG FAULT
std::cout << iActions.getClientDatasource() << std::endl;
//parse_test();
return 0;
}
int DataParse::parse_test(){
std::cout << '\t' << '\t' << iActions.getClientDatasource()
<<
std::endl;
return 0;
}
*
DataParse.h*
#ifndef DATAPARSE_H_
#define DATAPARSE_H_
#include <string>
#include <iostream>
#include "RetsParse.h"
#include "../config/InstConfig.h"
#include "../dbactions/DbConnect.h"
class DataParse {
InstConfig iActions;
public:
DataParse();
int parse_rets(std::string instanceUUID);
int parse_test();
};
#endif /* DATAPARSE_H_ */
*DbConnect.cpp*
#include "DbConnect.h"
mysqlpp::StoreQueryResult DbConnect::db_query(std::string sqlQuery){
// create database connection
mysqlpp::Connection sqlConn(false);
singleInst = Singleton::getInstance();
if(sqlConn.connect(singleInst->getDatasource().c_str(),singleInst->getHostname().c_str(),singleInst->getUsername().c_str(),singleInst->getPassword().c_str())){
// execute sql query
mysqlpp::Query query = sqlConn.query(sqlQuery);
if (mysqlpp::StoreQueryResult result = query.store()) {
sqlConn.disconnect();
return result;
}else{
std::cerr << "Failed to get item list: " << query.error()
<<
std::endl;
sqlConn.disconnect();
}
}else{
std::cerr << "DB connection failed: " << sqlConn.error() <<
std::endl;
sqlConn.disconnect();
}
}
std::string DbConnect::mysqlStringToString(mysqlpp::String inStr){
mysqlpp::String s = inStr;
std::string outStr = std::string(s.data(),s.length());
return outStr;
}
*DbConnect.h*
#ifndef DBCONNECT_H_
#define DBCONNECT_H_
#include "../common/Singleton.h"
#include <mysql++.h>
#include <string>
#include <iostream>
#include <sstream>
class DbConnect {
public:
DbConnect();
mysqlpp::StoreQueryResult db_query(std::string sqlQuery);
std::string mysqlStringToString(mysqlpp::String inStr);
};
#endif /* DBCONNECT_H_ */
Here is the output from strace
connect(3, {sa_family=AF_INET, sin_port=htons(3306),
sin_addr=inet_addr("192.168.1.102")}, 16) = 0
setsockopt(3, SOL_SOCKET, SO_RCVTIMEO,
"\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
setsockopt(3, SOL_SOCKET, SO_SNDTIMEO,
"\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
setsockopt(3, SOL_IP, IP_TOS, [8], 4) = 0
setsockopt(3, SOL_TCP, TCP_NODELAY, [1], 4) = 0
setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
read(3, "4\0\0\0\n5.0.45\0\367/\0\0eP>%?K>2\0,\242\10\2\0\0\0"..., 16384) =
56
write(3, "L\0\0\1\215\242\0\0\0\0\0@\10\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 80) =
80
read(3, "\1\0\0\2\376", 16384) = 5
write(3, "\t\0\0\3DF^TQGGW\0", 13) = 13
read(3, "\7\0\0\4\0\0\0\2\0\0\0", 16384) = 11
poll([{fd=3, events=POLLIN|POLLPRI}], 1, 0) = 0
write(3, "\244\1\0\0\3SELECT rcom_instances.usern"..., 424) = 424
read(3, "\1\0\0\1\7O\0\0\2\3def\rrcom_instance\16rcom"..., 16384) = 580
poll([{fd=3, events=POLLIN|POLLPRI}], 1, 0) = 0
write(3, "\1\0\0\0\1", 5) = 5
shutdown(3, 2 /* send and receive */) = 0
close(3) = 0
write(1, "7\n", 27
) = 2
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Process 22849 detached