【问题标题】:C++ Class Pointer Deletion (Segmentation Fault Issue)C++ 类指针删除(分段错误问题)
【发布时间】:2011-03-13 16:07:40
【问题描述】:

已解决

好吧,当我尝试删除我的类指针时,我似乎收到了分段错误。我已经尝试了很多东西,但还没有让它起作用。无论如何,这是我的 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;
} 

【问题讨论】:

  • 它叫做析构函数

标签: c++ class pointers segmentation-fault


【解决方案1】:

我没有看到任何初始化 lengthsresult 的代码,因此您可能正在删除野指针。

【讨论】:

    【解决方案2】:

    由于您从未调用过query,因此您正在对无效的result 指针进行操作。在eMysql构造函数中,将result初始化为NULL

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

    然后,在析构函数中:

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

    在析构函数中有一点看起来不对:

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

    但可能还有其他问题。

    • TextToSQL 是否拥有m 的所有权?
    • resultmysql~eMysql 中是否有效?
    • lengths 是作为数组分配的吗?使用delete [] lengths; 删除
    • 我假设这些是类成员,它们是否已初始化?

    以下是调试这些类型问题的几种方法(在 Linux 上):

    • 运行valgrind 以检查无效的内存引用。
    • 运行gdb,当它崩溃时输入bt

    【讨论】:

    • 我知道你可以删除 NULL ptrs,但这有什么区别呢? free 不一定会使 ptr 为 NULL - 它不能,它不是由 &result 传递的。这将是一个双重删除,这可能是一个段错误。
    • 哦,mysql_free_result 不是delete。因此,传递 NULL ptr 不一定是安全的。
    • 好的,当您的意思是 mysql_free_result 时,我将您的 freedelete 混淆了。立即查看。
    • 析构函数即使是空的也会返回这个错误,所以2被取消了,我删除了长度。
    • @Fellixombc :编辑了更多信息和调试助手的链接。
    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 2012-09-19
    • 2020-07-04
    • 2023-04-04
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多