views:

81

answers:

2

SOLVED

Well, it seems that I'm receiving a segmentation fault when I try deleting my class pointer. I've tried many things, but yet haven't got it to work. Anyways, here is my main.cpp: (this segmentation fault is occurring when I delete 'm')

    /*
 * Copyright(c)Fellixombc 2010
 *
 * TextToSqlRs is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TextToSqlRs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TextToSqlRs.  If not, see <http://www.gnu.org/licenses/&gt;.
 */

#include <ctime>
#include <iostream>
#include <unistd.h>
#include <string>
#include "eMysql.h"
#include "manage.h"

using namespace std;

int main() {
    time_t start, stop;
    double TIME;
    string host, user, password, database;
    string path;
    bool run = true;
    int runtime;
    int MAX;
    int max;


    cout << "Please enter the required information." << endl << "Absolute path to character folder: ";
    cin >> path;
    cout << "MySql Database Host: ";
    cin >> host;
    cout << "MySql Username: ";
    cin >> user;
    cout << "MySql Password: ";
    cin >> password;
    cout << "Database name: ";
    cin >> database;

    start = clock();
    eMysql* m = new eMysql;
    Manage* manage = new Manage;
    m->connect(host.c_str(), user.c_str(), password.c_str(), database.c_str());
    manage->ReadDir(path);
    manage->TextToSql(m);

    delete manage;
    delete m;

    stop = clock();

    TIME = ((double)stop - (double)start)/CLOCKS_PER_SEC;
    cout << "Finished in " << TIME << " seconds" << endl; }

eMysql deconstructor:

eMysql::~eMysql() {
mysql_free_result(result);
mysql_close(&mysql);

if(lengths) delete lengths;
if(result) delete result; }

mysql/result header:

#ifndef _MYSQL_H
#define _MYSQL_H

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

class eMysql {
public:
    eMysql();
    int connect(const string, const string, const string, const string);
    int query(string query);
    int strip(string&);
    int rows();
    virtual ~eMysql();
private:
    MYSQL mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;
    unsigned int i;
};

#endif  /* _MYSQL_H */

mysql initializer

eMysql::eMysql() {
    mysql_init(&mysql);
}

result initializer

int eMysql::query(string query) {
    if(mysql_query(&mysql, query.c_str())) {
        cout << "Failed to run query: " << mysql_error(&mysql) << endl;
        exit(1);
    }
    result = mysql_store_result(&mysql);
    query.erase();
    return 1;
} 
+1  A: 

I don't see any code that initializes lengths or result, so you might be deleting wild pointers.

Ben Voigt
+1  A: 

Since you never call query, you're operating on an invalid result pointer. In the eMysql constructor, initialize result to NULL:

eMysql::eMysql() : result(NULL) {
  // ...
}

Then, in the destructor:

eMysql::~eMysql() {
  if (result) mysql_free_result(result);
  mysql_close(&mysql);
}

One thing looks wrong in the destructor:

eMysql::~eMysql() {
  // You use "result" here, but later check if it's not NULL.
  // You should only free if it's not null.
  mysql_free_result(result);
  mysql_close(&mysql);

  if(lengths) delete lengths;
  // Are you sure you need to delete this?  Isn't that what the
  // mysql_free_result is for?  This will still be a non-NULL ptr
  // that has already been free'd.  This might be a double-delete
  // which would likely manifest as a segmentation fault.
  if(result) delete result;
}

But there could be other problems.

  • Does TextToSQL take ownership of m?
  • Are result and mysql valid in the ~eMysql?
  • Is lengths allocated as an array? Delete with delete [] lengths;
  • I assume these are class members, are they initialized at all?

Here are a couple methods for debugging these types of problems (on Linux):

  • Run valgrind to check for invalid memory references.
  • Run gdb, type bt when it crashes.
Stephen
From what is shown here the most likely candidate is your (2). And btw, one can savely `delete` `NULL` pointers.
honk
Stephen
Oh, and `mysql_free_result` isn't `delete`. So, it's not necessarily safe to pass a NULL ptr.
Stephen
OK, I was confusing your `free` for `delete` when you meant `mysql_free_result`. See now.
honk
The destructor still returns this error even if its empty, so 2 is canceled out, I removed lengths.
Fellixombc
@Fellixombc : Edited with some more info and links to debugging helpers.
Stephen
@Fellixombc : Then it looks like it's related to `result` or `mysql`. You should post the code that initializes those.
Stephen
Alright, I will give that a shot, if else fails, I guess I could rewrite the application.
Fellixombc
alright updated the post Stephen, the code is at the bottom of the post.
Fellixombc
@Fellixombc : Edited in response.
Stephen
Still no luck, but I do run the query actually, its just in another file.
Fellixombc
I'm deleting my answer since yours is definitely better at this point. I wanted to post my last comment though: does it make a difference if you do mysql = mysql_init( - I've never seen it like that and like to explicitly indicate that no parameters are passed to the constructor.
SB
I actually found the solution. I made a mistake in the other file delete mysql; so, the pointer was already deleted, but before when I was debugging and I deleted that, I still got this error because I believe I was deleting result although it was already deleted. Thank you everyone for the help!
Fellixombc
@SB : Thanks, unfortunately not good enough ;) @Fellixombc : Glad you found the issue!
Stephen
@SB, if there are no parameters required in your constructor, you leave the parentheses off. Well, that is what I read somewhere.
Fellixombc
you did the best you could with the information you had. how were you to know that there was another file? :)
SB