【问题标题】:Getting read access violation exception in VC++ how to handle this exception?在 VC++ 中获取读取访问冲突异常如何处理此异常?
【发布时间】:2016-07-25 13:44:57
【问题描述】:

我正在使用 MS Visual Studio 2015 开发一个使用 VC++ 和后端作为 SQLite 的小型应用程序。但是使用标准 SQLite3 C api 没有发生异常。

但是当我尝试为使用 SQLite 制作一个小包装器时。为了简化将函数用作 SQLite API,我制作了一个头文件。我收到read access violation 异常。 如何处理这个异常以及我应该在我的小包装器中进行哪些更改,以便我可以在应用程序的多个模块中使用它。

这是我的小包装器SQLite.cpp

#include "inc\sqlite3.h"
#include <string.h>
#pragma once

class SQLiteConnection {
    sqlite3 * conn;
public:
       SQLiteConnection() {
            conn = NULL;
        }
       ~SQLiteConnection() {
            sqlite3_close(conn);
        }

        int connect(char const * dbName) {

            int res = sqlite3_open(dbName, &conn);
            if (SQLITE_OK != res) {
                 printf("%s\n", sqlite3_errmsg(conn));
                 return res;
            }
            return res;
        }
        sqlite3 * getConn() {
            return conn;
        }
};

class Statement {
       sqlite3_stmt * stmt;
 public:
     Statement() {
         stmt = NULL;
     }
     int prepare(sqlite3 *,char *);
     int bind_param_int(sqlite3 *,int , int);
     int bind_param_text(sqlite3 * ,int , char const *);
     int bind_param_double(sqlite3 * ,int , double);
     bool step();
     int reset();
     char const * getColText(int idx);
     void finalize() {
         sqlite3_finalize(stmt);
     }
};
int Statement::prepare(sqlite3 * conn, char *sql) {
    int result;
    result = sqlite3_prepare_v2(conn, sql, -1, &stmt, NULL);

    if (SQLITE_OK != result) {
        sqlite3_errmsg(conn);
        return 0;           
    }
    return SQLITE_OK;
}
int Statement::bind_param_int(sqlite3 * conn,int idx, int val) {
     int res;
     res = sqlite3_bind_int(stmt, idx, val);
     if (SQLITE_OK != res) {
         sqlite3_errmsg(conn);
         return 0;
      }
      return SQLITE_OK;
}

int Statement::bind_param_text(sqlite3 * conn, int idx, char const * val) {
      int res;
      res = sqlite3_bind_text(stmt, idx, val, strlen(val)+1, SQLITE_STATIC);
      if (SQLITE_OK != res) {
          sqlite3_errmsg(conn);
          return 0;
      }
      return SQLITE_OK;
}
int Statement::bind_param_double(sqlite3 * conn , int idx, double val) {
      int res;
      res = sqlite3_bind_double(stmt, idx, val);
      if (SQLITE_OK != res) {
           sqlite3_errmsg(conn);
           return 0;
       }
       return SQLITE_OK;
}
bool Statement::step() {
      int res = sqlite3_step(stmt);
      if (SQLITE_DONE == res) return true;
      if (SQLITE_ROW == res) return true;
      return false;
}
int Statement::reset() {
      int res = sqlite3_reset(stmt);
      if (SQLITE_OK == res) return res;
      return 0;
}
char const * Statement::getColText(int idx) {
     return (char const *)sqlite3_column_text(stmt, idx);
}

这是我的主要 app.cpp 文件

#include <iostream>
#include <stdio.h>
using namespace std;
/* 
* SQLite3 header file
* for getting Constants for verification of results.
*/
 #include "inc\sqlite3.h"
 #include "SQLite.h"

int main() {
    SQLiteConnection con;
    try {
        if (SQLITE_OK == con.connect(":memory:")) {
            cout << "Connected to DB";
            Statement stmt;
            if (SQLITE_OK == stmt.prepare(con.getConn(), "select 'Hello World'")) {
                while (stmt.step())
                {
                    cout << "\n" << stmt.getColText(0) << "\n";
                }
                stmt.finalize();
            }
        }
        else {
            return 1;
        }
    }
    catch (const exception & e) {
        cout << "Exception..."<< e.what();
    }
    getchar();
    return 0;
}

第一次接触 Visual C++SQLite3 所以知识水平是初学者,我对现代 C++ 和 STL 也不太了解;(很快就会学习。 . 希望聪明的头脑能向我解释这里发生了什么,以及我将如何摆脱困境。

【问题讨论】:

  • 您正在运行 Visual C++,它为 Windows 平台上的用户模式应用程序提供了世界上最好的集成调试器之一。 使用它。它将向您展示有关代码崩溃的方式/原因的惊人事情。
  • 试试this
  • 用 F11 和 F10 启动和调试程序,看看是哪个函数抛出了异常

标签: c++ visual-c++ exception-handling sqlite nullpointerexception


【解决方案1】:

读取访问冲突异常。

我不认为这是一个 C++ 异常,而是由于您的代码尝试访问它不应该访问的内存而导致的硬件异常。它应该使用附加信息(如地址)生成,这可能会提示问题的原因。

如何处理这个异常

好吧,你不会 :-) 或者你不能,你的应用程序必须崩溃。您实际上可能会编写异常处理程序,该处理程序可以将堆栈跟踪写入某个日志文件,或者制作转储文件以供以后分析。在大型应用程序中,程序员会添加大量日志记录,至少允许测试人员向他们发送日志文件,这可能有助于发现崩溃发生时应用程序正在做什么。

以及我应该在我的小包装器中进行哪些更改,以便我可以在应用程序的多个模块中使用它。

很难说,因为当它崩溃时您应该使用调试器来查找位置。也许您正在使用一些未初始化的内存,或者错误地使用 API。您的 select 语句看起来很奇怪,对吗?但可能它不应该使应用程序崩溃。您的析构函数看起来也很可疑,即使 conn 为 NULL,也会调用 sqlite3_close(conn);

【讨论】:

    【解决方案2】:

    如果这与您说即使 SQLite 返回 SQLITE_DONE 也尝试获取值的其他问题有关,那么这就是答案。使用该代码,步进已经完成,没有什么可阅读的,因此无论如何阅读都可能导致严重的崩溃。您只能使用代码SQLITE_ROW阅读。

    即使单步执行完成,您的代码也会在单步执行时返回 true,请将其从代码中删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2013-11-22
      • 1970-01-01
      • 2023-03-15
      • 2011-09-28
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多