Unhandled exception at 0x00b21840 in tester.exe: 0xC0000005: Access violation reading location 0x00000000
BOOL bLoadActions()
{
int iIndex = 0;
int iRows;
int iFields;
MYSQL_ROW myRow;
MYSQL_FIELD* field [20开发者_开发知识库0];
for (int a = 0; a < 200; a++)
field[a] = NULL;
mysql_query(con, "SELECT * FROM `cq_action`;");
res= mysql_store_result(con);
if ((iRows = (int)mysql_num_rows(res)) > 0)
{
iFields = (WORD)mysql_num_fields(res);
for(int b = 0; b < iRows; b++)
{
myRow = mysql_fetch_row(res);//<=== error here
mysql_field_seek(res, 0);
for(int f = 0; f < iFields; f++)
{
field[f] = mysql_fetch_field(res);
if(field[f]) {
if(!strcmp(field[f]->name, "id"))
{
iIndex++;
m_pActionList[iIndex] = new class CAction;
m_pActionList[iIndex]->id = atoi(myRow[f]);
m_pActionList[iIndex]->index = iIndex;
}
else if(!strcmp(field[f]->name, "param"))
{
m_pActionList[iIndex]->param = new char[strlen(myRow[f])+1];
m_pActionList[iIndex]->param[strlen(myRow[f])] = 0;
memcpy(m_pActionList[iIndex]->param, myRow[f], strlen(myRow[f]));
}
else if(!strcmp(field[f]->name, "type"))
m_pActionList[iIndex]->type = atoi(myRow[f]);
else if(!strcmp(field[f]->name, "id_next"))
m_pActionList[iIndex]->idnext = atoi(myRow[f]);
else if(!strcmp(field[f]->name, "id_nextfail"))
m_pActionList[iIndex]->idfail = atoi(myRow[f]);
else if(!strcmp(field[f]->name, "data"))
m_pActionList[iIndex]->data = atoi(myRow[f]);
}
else{
if(field[f]=NULL)
cout<<"error"<<endl;
}
}
}
}
return 0;
} idk every thing seems good.... but i get this error
Unhandled exception at 0x00b21840 in tester.exe: 0xC0000005: Access violation reading location 0x00000000.
after the edit the errors happens here myRow = mysql_fetch_row(res);//<=== error here
It means one of the pointers is null when it is dereferenced. Attach a debugger, it will tell you which line the error occurs on.
As others said, you're most likely dereferencing a NULL
pointer coming from mysql_fetch_field
. You should change your code to something like this:
for(int f = 0; f < m_fields; f++){
field[f] = mysql_fetch_field(res);
if(field[f]) {
if(!strcmp(field[f]->name, "id")){// <= error happens here
iIndex++;
m_pNpcList[iIndex] = new class CNpc;
m_pNpcList[iIndex]->m_iID = atoi(row[f]);
}
} else {
//field[f] is NULL, do some error handling here
}
}
精彩评论