gpt4 book ai didi

c++ - "0xC0000005: Access violation"使用 fstream 时

转载 作者:行者123 更新时间:2023-11-28 08:19:30 29 4
gpt4 key购买 nike

我知道有很多关于访问冲突的问题,但我已经阅读了很多,但似乎仍然无法破解这一问题。我正在将这段代码从 Java 翻译成 C++,而我对 C++ 的经验要少得多,所以这段代码可能有点(又名非常)不稳定。

我的代码在这一行中断(在 setup_next_buff_reader 中):

this->buff_reader = new fstream(file_path,fstream::in|fstream::out);

然后跳转到fstream中的close()函数。

我知道我处理指针、分配/取消分配内存等的方式可能有问题,但我不知道哪里有问题。非常感谢对此的任何帮助。

bool TabDelimitedSource::setup_next_buff_reader() {

// Sets up the lucene analyzer and tries to load the next file in
// this->data_source_files, resetting counters/flags as needed

this->current_source_file += 1;
bool no_more_files = false; // assume we have no more files by default

if(this->current_source_file < sizeof(this->data_source_files)){
const char* file_path = (this->data_source_files[this->current_source_file]).c_str();
cout << "File Path: " <<file_path <<"\n";
this->buff_reader = new fstream(file_path,fstream::in|fstream::out);
}
else{
no_more_files = true;
}

return no_more_files; // let the caller know whether there was another file or not
}


TabDelimitedSource::TabDelimitedSource(){}

TabDelimitedSource::TabDelimitedSource(const char* dir_path) {
// Load all of the files in the directory using the file name filter provided
//fstream * data_source_dir = new fstream("path.txt");
// NOTE: To change what files are loaded, use a different Filter here
vector<string> files;
this->getdir( dir_path, files);
this->data_source_files = files;
open();
cout << "Went through the TDSource constructor.\n";
}

void TabDelimitedSource::setColumnHoldingDoc(int column){
// Allow the user to specify which column then want to extract
this->column_holding_doc = column;
}

void TabDelimitedSource::open() {
this->current_row = -1;
this->current_source_file = -1;
this->setup_next_buff_reader(); // setup first data source
}

void TabDelimitedSource::close() {
try{
this->buff_reader->close();
this->buff_reader->clear();
}
catch (ifstream::failure e){
; // ignore this exception
}
}

void TabDelimitedSource::reset(){
this->close();
this->open();
}

vector<string> TabDelimitedSource::getNext() {
// Returns the next document (a given cell) from the file(s)
char row[256]; // Return NULL if no more documents/rows
vector<string> document;

try{
this->buff_reader->getline(row, 256);
cout <<"Row: "<< row << "\n";
}catch (ifstream::failure e){
; // Ignore and fall through
}

if (buff_reader->eof()){
this->current_row += 1;
vector<string> cells;
this->split(row, "\t", cells); // Split the row on tabs
// Split the cell into individual words using the lucene analyzer
try{
string original_document = cells.at(this->column_holding_doc);
}catch (ifstream::failure e){
cout<<"Row " << this->current_row << " did not have column " << this->column_holding_doc;
}
}
else{
// We're at the end of the current file, try loading the next one
bool no_more_files = this->setup_next_buff_reader();
// If there was another file to load, recurse to get its first document
if (!no_more_files){
// Don't waste time doing Array->ArrayList->Array conversion
return this->getNext();
}
}

// Return our arrayList as an array... there has to be a better way to do this
vector<string> return_val ;
if(!document.empty()){ // return NULL by default
for(int i=0; i<(int)document.size(); i++){
return_val.push_back(document[i]);
}
}

return return_val;
}

void TabDelimitedSource::split(const string& str, const string& delim, vector<string>& result){
size_t start_pos = 0;
size_t match_pos;
size_t substr_length;

while((match_pos = str.find(delim, start_pos)) != string::npos){
substr_length = match_pos - start_pos;

if (substr_length > 0){
result.push_back(str.substr(start_pos, substr_length));
}
start_pos = match_pos + delim.length();
}

substr_length = str.length() - start_pos;

if (substr_length > 0){
result.push_back(str.substr(start_pos, substr_length));
}
}

int TabDelimitedSource::getdir(string dir, vector<string> &files){
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}

while ((dirp = readdir(dp)) != NULL) {
files.push_back((const char*)(dirp->d_name));
}
closedir(dp);
return 0;
}

TabDelimitedSource::~TabDelimitedSource(){}

这是调用堆栈:

msvcp90d.dll!std::basic_filebuf<char,std::char_traits<char> >::close()  Line 232 + 0x3 bytes    C++
msvcp90d.dll!std::basic_fstream<char,std::char_traits<char> >::close() Line 971 + 0xb bytes C++
Testing.exe!TabDelimitedSource::close() Line 98 + 0xe bytes C++
Testing.exe!TabDelimitedSource::setup_next_buff_reader() Line 26 C++
Testing.exe!TabDelimitedSource::open() Line 94 C++
Testing.exe!TabDelimitedSource::TabDelimitedSource(const char * dir_path=0x09800fa8) Line 76 C++
Testing.exe!Query::load_data_source(const char * data_path=0x09800fa8, std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_database="c:/Users/dfink.LISSARD2/Documents/Visual Studio 2008/Projects/Testing/jieddo.mdb", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_dbtable="jieddo_v2", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_id="report_id", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_text="summary", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_date="occured_when", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_filter="event_type='Explosive Hazard' ", int tab_delim_column=1) Line 144 + 0x38 bytes C++
Testing.exe!Query::model(const char * path=0x09800fa8, std::basic_string<char,std::char_traits<char>,std::allocator<char> > database="c:/Users/dfink.LISSARD2/Documents/Visual Studio 2008/Projects/Testing/jieddo.mdb", std::basic_string<char,std::char_traits<char>,std::allocator<char> > dbtable="jieddo_v2", std::basic_string<char,std::char_traits<char>,std::allocator<char> > field_id="report_id", std::basic_string<char,std::char_traits<char>,std::allocator<char> > field_text="summary", std::basic_string<char,std::char_traits<char>,std::allocator<char> > field_date="occured_when", std::basic_string<char,std::char_traits<char>,std::allocator<char> > filter="event_type='Explosive Hazard' ", int tab_delim_column=1) Line 72 C++
Testing.exe!Query::Query(bool run_batch_job=true, std::basic_string<char,std::char_traits<char>,std::allocator<char> > datasource="tab-delimited-source", char * data_path=0x09800fa8, std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_database="c:/Users/dfink.LISSARD2/Documents/Visual Studio 2008/Projects/Testing/jieddo.mdb", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_dbtable="jieddo_v2", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_id="report_id", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_text="summary", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_field_date="occured_when", std::basic_string<char,std::char_traits<char>,std::allocator<char> > jdo_filter="event_type='Explosive Hazard' ", int tab_delim_column=1) Line 55 C++
Testing.exe!main() Line 209 + 0x1c3 bytes C++
Testing.exe!__tmainCRTStartup() Line 582 + 0x19 bytes C
Testing.exe!mainCRTStartup() Line 399 C
kernel32.dll!762e3677()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77399f02()
ntdll.dll!77399ed5()

我调用TabDelimitedSource的地方:

bool Query::load_data_source(const char* data_path, string jdo_database, 
string jdo_dbtable, string jdo_field_id,
string jdo_field_text, string jdo_field_date,
string jdo_filter, int tab_delim_column){
// Attempts to load the data source (to this.ds) based on the datasource name
// NOTE: This will write over the old data source if it exists!
// data_path: Where the source files are located
// jdo_*: Argument specific to Jieddo data source
// tab_delim_colum: Which column to extract from a tab delimited source file
bool load_successful = true;
try{
if (this->datasource.compare("tab-delimited-source") == 0) {
this->ds = new TabDelimitedSource(data_path);
this->ds->setColumnHoldingDoc(tab_delim_column);
string pref_text_analyzer;
this->ds->setColumnHoldingDoc(tab_delim_column);
}
else {
load_successful = false;
}
}
catch(ifstream::failure e){
cout<<"FAIL."<<"n";
load_successful = false;
}
return load_successful;
}

编辑:抱歉,我忘了我添加了两行代码(我把它们取了回来)。该代码实际上确实在上述同一行中断,但它进入了解锁函数中的 mlock.h。

最佳答案

假设 data_source_files 是一个字符串 vector ,这个测试

if(this->current_source_file < sizeof(this->data_source_files)){  

检查当前索引是否小于 std::vector 对象中的字节数,而不检查它是否小于 vector 中包含的字符串数。那将是

data_source_files.size()

这很可能会导致超出范围的访问。

关于c++ - "0xC0000005: Access violation"使用 fstream 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430494/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com