segmentation fault when deleting class

Read below.
Last edited on
This: delete manage, m; does not do what you think it does. It will only delete m and will ignore manage.

You need to do this:

1
2
delete manage;
delete m;
hmm, I'm getting a segmentation fault when I delete m.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * 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/>.
 */

#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;
}


deconstructor:
1
2
3
4
5
6
7
eMysql::~eMysql() {
    mysql_free_result(result);
    mysql_close(&mysql);
    if(host) delete host; if(user) delete user; if(pass) delete pass;
    if(database) delete database; if(lengths) delete lengths;
    if(result) delete result;
}


It has something to do with the above statement.
Might I suggest using std::string in place of char* ?
Alright, but I commented out all the deletions in the deconstructor, and yet I still get it. Any ideas on why?
Topic archived. No new replies allowed.