这种内存问题最头疼了好不啦!Unhandled Exception at XXXXX in xxx.exe Access Violation!

fzlrpsun 2012-07-19 07:57:32

void PatientDatebase::SortPatientByLastname()
{
database.sort(greater<Patient>());
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
}

void main(string outfile)
{
PatientDatebase pData;
cout<<"sort patients by last name: "<<endl;
pData.SortPatientByLastname();
}


错误信息
'Patient.exe': Loaded 'C:\Users\lrp\Documents\Visual Studio 2010\Projects\Patient\Debug\Patient.exe', Symbols loaded.
'Patient.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'Patient.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'Patient.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'Patient.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'Patient.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.
Unhandled exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.
First-chance exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.
Unhandled exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.
First-chance exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.
Unhandled exception at 0x0ff4ad4a (msvcp100d.dll) in Patient.exe: 0xC0000005: Access violation reading location 0x00000005.

错误出处
call stack window 指示> msvcp100d.dll!std::_Container_base12::_Orphan_all() Line 201 + 0x12 bytes

在xutility中

inline void _Container_base12::_Orphan_all()
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);

for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)//在这行出错
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
...全文
1083 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fzlrpsun 2012-07-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

在VC6下验证了你的代码,没能重现你遇到的问题。
执行的很顺利。
[/Quote]
谢了,我是2010的,运行结束后会直接无响应,调试才出现的那个错误,我把mian函数的参数去掉就没问题了
hyqok 2012-07-22
  • 打赏
  • 举报
回复
在VC6下验证了你的代码,没能重现你遇到的问题。
执行的很顺利。
oldmtn 2012-07-20
  • 打赏
  • 举报
回复
我这边编译没问题啊,,
不过我把
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
这部分注释了。

你的代码好像有2个main函数。。
fzlrpsun 2012-07-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

我这边编译没问题啊,,
不过我把
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
这部分注释了。

你的代码好像有2个main函数。。
[/Quote]

这个奇怪了,我把main的参数string outfile放到函数体里就没问题了。。。
fzlrpsun 2012-07-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

我这边编译没问题啊,,
不过我把
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
这部分注释了。

你的代码好像有2个main函数。。
[/Quote]

编译是没问题,可运行到最后就错误,程序停止工作,调试发现Unhandled Exception错误
patient.txt 中内容
chuck bass 1 grace 30
Jack Brown 2 grace 30
Judy L 3 Tim 31
Tom M 4 John 33
fzlrpsun 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

把你的代码帖上来啊。。
[/Quote]

谢谢
fzlrpsun 2012-07-19
  • 打赏
  • 举报
回复
patient.h

#include<string>
#include <list>
using namespace std;
struct Patient
{
public:
Patient();
Patient(string first_name,string last_name,int SIN,string doc_name,int ID);
string first_name,last_name,doc_name;
int SIN,ID;
friend bool operator>(const Patient& p1,const Patient& p2);
friend bool operator<(const Patient& p1,const Patient& p2);
friend bool operator==(const Patient& p1,const Patient& p2);
friend bool operator!=(const Patient& p1,const Patient& p2);

};

class PatientDatebase
{
public:
PatientDatebase();
bool LoadDatabase();
bool SaveDatebase(string filenaem);
bool AddPatient(Patient patient);
bool AddPatient(string first_name,string last_name,int SIN,string doc_name,int ID);
bool DeletePatient(int sin);
void SortPatientByLastname();
void PrintSameID(int ID);
Patient* SearchPatient(int sin);
private:
list<Patient> database;
};


patient.cpp

// Patient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Patient.h"
#include <fstream>
#include <iostream>
#include <Windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Patient::Patient()
{
}
Patient::Patient(string first_name,string last_name,int SIN,string doc_name,int I)
{
first_name=first_name;
last_name=last_name;
SIN=SIN;
doc_name=doc_name;
ID=ID;
}
bool operator<(const Patient& p1,const Patient& p2)
{
int i=0;
while((i<p1.last_name.length())&(i<p2.last_name.length()))
{
if(p1.last_name[i]<p2.last_name[i])
{
return true;
}
if(p1.last_name[i]>p2.last_name[i])
{
return false;
}
++i;
}
}

bool operator>(const Patient& p1,const Patient& p2)
{
int i=0;
while((i<p1.last_name.length())&(i<p2.last_name.length()))
{
if(p1.last_name[i]<p2.last_name[i])
{
return false;
}
if(p1.last_name[i]>p2.last_name[i])
{
return true;
}
++i;
}
}

bool operator==(const Patient& p1,const Patient& p2)
{
return ((p1.last_name == p2.last_name) && (p1.first_name == p2.first_name) && (p1.SIN == p2.SIN) && (p1.doc_name == p2.doc_name) && (p1.ID == p2.ID));
}
bool operator!=(const Patient& p1,const Patient& p2)
{
return !((p1.last_name == p2.last_name) && (p1.first_name == p2.first_name) && (p1.SIN == p2.SIN) && (p1.doc_name == p2.doc_name) && (p1.ID == p2.ID));
}

PatientDatebase::PatientDatebase()
{

}
bool PatientDatebase::LoadDatabase()
{
const int MAX_LINE_BUF=25;
char lineBuf[MAX_LINE_BUF];
char firstname[MAX_LINE_BUF],lastname[MAX_LINE_BUF],doc_name[MAX_LINE_BUF];
int sin,id,iRet;
ifstream iDatabase;
iDatabase.open("C:/Users/lrp/Desktop/patient.txt");
Patient patient;
if (!iDatabase.is_open() )
{
cout<<"open error"<<endl;
return false;
}

while( iDatabase.getline(lineBuf, MAX_LINE_BUF, '\n') )
{
if(lineBuf[0]==' ')
{
continue;
}
iRet = sscanf(lineBuf,"%s %s %d %s %d",firstname,lastname,&sin,doc_name,&id);
if(iRet!=5)
{
continue;
}
patient.first_name=firstname;
patient.last_name=lastname;
patient.SIN=sin;
patient.doc_name=doc_name;
patient.ID=id;
database.push_back(patient);
}

list<Patient>::iterator it = database.begin();
for(it = database.begin(); it != database.end(); ++it)
{

cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;

}
return true;
}

bool PatientDatebase::SaveDatebase(string filename)
{
string buf,tempstr;
char temp[4];
list<Patient>::iterator it;

ofstream outfile(filename.c_str());
for(it=database.begin();it!=database.end();++it)
{
buf.append(it->first_name+"\t");
buf.append(it->last_name+"\t");
itoa(it->SIN,temp,10);
tempstr=temp;
buf.append(tempstr+"\t");
buf.append(it->doc_name+"\t");
itoa(it->ID,temp,10);
tempstr=temp;
buf.append(tempstr+"\n");

outfile.write(buf.c_str(), buf.length());
buf.clear();

}

return true;
}

bool PatientDatebase::AddPatient(Patient patient)
{
database.push_back(patient);
return true;
}
bool PatientDatebase::AddPatient(string first_name,string last_name,int SIN,string doc_name,int ID)
{
Patient p;
p.first_name=first_name;
p.last_name=last_name;
p.SIN=SIN;
p.doc_name=doc_name;
p.ID=ID;
database.push_back(p);
return true;
}
bool PatientDatebase::DeletePatient(int sin)
{
list<Patient>::iterator it = database.begin();
while(it!=database.end())
{
if(it->SIN == sin)
{
// be careful here, database.erase(it) returns ++it.
// it's very easy to fall into the trap of infinite loop
it = database.erase(it);
//return true;
}
else
{
++it;
}
}
return true;
}
void PatientDatebase::SortPatientByLastname()
{
database.sort(greater<Patient>());
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
}

void PatientDatebase::PrintSameID(int ID)
{
list<Patient>::iterator it;
for(it = database.begin(); it != database.end(); ++it)
{
if(it->ID ==ID)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
else
{
continue;
}
}
}

Patient* PatientDatebase::SearchPatient(int sin){
list<Patient>::iterator it = database.begin();
while(it!=database.end())
{
if(it->SIN == sin)
{
cout << '\t' << it->first_name << '\t' << it->last_name << '\t' << it->SIN << '\t' << it->doc_name << '\t' << it->ID << endl;
}
++it;
}
return NULL;
}

void main(string outfile)
{
cout<<"please input the filename"<<endl;
// cin>>infile;
// infile="patient.txt";

PatientDatebase pData;

pData.LoadDatabase();
cout<<"please input patient's SIN"<<endl;
int sin;
cin>>sin;
pData.SearchPatient(sin);
cout<<"delete ? Y/N"<<endl;
string s;
cin>>s;
if(s=="Y")
{
cout<<"deleting"<<endl;
Sleep(3000);
pData.DeletePatient(sin);
}

cout<<"please input the outfile name"<<endl;
string out,location;
location="C:/Users/lrp/Desktop/";
cin>>out;
outfile=location.append(out);
pData.SaveDatebase(outfile);
int ID;
cout<<"please input doctor's ID"<<endl;
cin>>ID;
cout<<"Patients cared by doctor "<<ID<<":"<<endl;
pData.PrintSameID(ID);

cout<<"sort patients by last name: "<<endl;
pData.SortPatientByLastname();
}

oldmtn 2012-07-19
  • 打赏
  • 举报
回复
把你的代码帖上来啊。。
fzlrpsun 2012-07-19
  • 打赏
  • 举报
回复
这个错误是在程序运行结束时产生的,就是main函数的右大括号的时候

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧