C++类指针删除(分段错误问题)
已解决
嗯,当我尝试删除类指针时,我似乎收到了分段错误。我已经尝试了很多事情,但还没有发挥作用。无论如何,这是我的 main.cpp:(当我删除“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/>.
*/
#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 解构器:
eMysql::~eMysql() {
mysql_free_result(result);
mysql_close(&mysql);
if(lengths) delete lengths;
if(result) delete result; }
mysql/结果标头:
#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 初始值设定项
eMysql::eMysql() {
mysql_init(&mysql);
}
结果初始值设定项
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;
}
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/>.
*/
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到任何初始化
lengths
或result
的代码,因此您可能正在删除野指针。I don't see any code that initializes
lengths
orresult
, so you might be deleting wild pointers.由于您从未调用
query
,因此您正在操作无效的result
指针。在eMysql
构造函数中,将result
初始化为NULL
:然后,在析构函数中:
析构函数中有一件事看起来不对:
但可能还有其他问题问题。
TextToSQL
是否拥有m
的所有权?result
和mysql
在~eMysql
中有效吗?lengths
是否分配为数组?使用delete [] lengths; 删除;
以下是调试此类问题的几种方法(在 Linux 上):
bt
。Since you never call
query
, you're operating on an invalidresult
pointer. In theeMysql
constructor, initializeresult
toNULL
:Then, in the destructor:
One thing looks wrong in the destructor:
But there could be other problems.
TextToSQL
take ownership ofm
?result
andmysql
valid in the~eMysql
?lengths
allocated as an array? Delete withdelete [] lengths;
Here are a couple methods for debugging these types of problems (on Linux):
bt
when it crashes.