在VC ++中获取读取访问冲突异常如何处理该异常? [英] Getting read access violation exception in VC++ how to handle this exception?

查看:434
本文介绍了在VC ++中获取读取访问冲突异常如何处理该异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MS Visual Studio 2015 使用 VC ++ 开发一个小型应用,并以 SQLite 作为后端。但是,使用标准SQLite3 C api不会发生任何异常。



但是当我尝试为使用SQLite做一个小型包装时。为了简化将函数用作SQLite API的过程,我制作了一个头文件。我收到违反读访问权限的异常。
如何处理该异常以及我应该在小型包装程序中进行哪些更改,以便可以在应用程序的多个模块中使用它。



这是我的小包装器 SQLite.cpp

  #include inc\sqlite3.h 
#include< string.h>
#pragma一次

类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;
}
sqlite3 * getConn(){
return conn;
}
};

类别声明{
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语句:: prepare(sqlite3 * conn,char * sql){
int结果;
结果= sqlite3_prepare_v2(conn,sql,-1,& stmt,NULL);

if(SQLITE_OK!=结果){
sqlite3_errmsg(conn);
返回0;
}
返回SQLITE_OK;
}
int语句:: 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);
返回0;
}
返回SQLITE_OK;
}

int语句:: 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);
返回0;
}
返回SQLITE_OK;
}
int语句:: 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);
返回0;
}
返回SQLITE_OK;
}
bool Statement :: step(){
int res = sqlite3_step(stmt);
如果(SQLITE_DONE == res)返回true;
如果(SQLITE_ROW == res)返回true;
返回false;
}
int语句:: reset(){
int res = sqlite3_reset(stmt);
如果(SQLITE_OK == res)返回res;
返回0;
}
char const * Statement :: getColText(int idx){
return(char const *)sqlite3_column_text(stmt,idx);
}

这是我的主要 app.cpp 文件

  #include< iostream> 
#include< stdio.h>
使用命名空间std;
/ *
* SQLite3头文件
*用于获取用于验证结果的常量。
* /
#include inc\sqlite3.h
#include SQLite.h

int main(){
SQLiteConnection con ;
try {
if(SQLITE_OK == con.connect(:memory:))){
cout<< 已连接到数据库;
语句stmt;
if(SQLITE_OK == stmt.prepare(con.getConn(),选择'Hello World')){
而(stmt.step())
{
cout<< \n<< stmt.getColText(0)<< \n;
}
stmt.finalize();
}
}
else {
返回1;
}
}
捕获(常量例外& e){
cout<< 例外...<< e.what();
}
getchar();
返回0;
}

Visual C ++ 中首次启动> SQLite3 ,所以知识水平是新手,我对现代C ++和STL也不太了解;(很快就会学到。。
希望能有出色的头脑向我解释这里发生了什么以及如何我可以从中摆脱出来。

解决方案

如果这与您说要尝试获取值的其他问题有关,当SQLite返回 SQLITE_DONE 时,这就是答案。使用该代码,步进已完成,没有任何读取内容,因此无论如何读取都可能导致严重的崩溃。代码 SQLITE_ROW



即使步进已完成,您的代码也将返回true,请从代码中删除。 / p>

I'm using MS Visual Studio 2015 to develop a small app using VC++ and backend as SQLite. However using standard SQLite3 C api there are no exceptions occurring.

But when i tried to make a small wrapper for using SQLite. I made one header file for simplification for using functions as SQLite APIs. I'm getting read access violation exception. How to handle this exception and what changes i should make in my small wrapper so i can use it in multiple modules of the app.

here is my small wrapper 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);
}

Here is my main app.cpp file

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

Started first time in Visual C++ and SQLite3 so the knowledge level is beginner and I also don't know much about Modern C++ and STL ;( will learn soon.. Hoping that brilliant minds will explain me what is happening here and how will I be able to get out from that.

解决方案

If this is related to your other question where you say you tried getting values even when SQLite returns SQLITE_DONE then that is the answer. With that code the stepping has finished and there is nothing to read so reading anyway may cause nasty crashes. You may only read with the code SQLITE_ROW.

Your code returns true on step even when stepping is finished, remove that from the code.

这篇关于在VC ++中获取读取访问冲突异常如何处理该异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆