I am writing a ODBC-database class which contains a member function used to fetch a series of attributes and tuples from a given query.
I have a single line of code in the statements below which causes this runtime error to be throw in debug mode:
Unhandled exception at <mem loc> in <prog name>: 0xC0000005: Access violation writing location <mem loc>.
and here is the code where ERROR points out the offending line:
SQLINTEGER length = 0;
vector<vector<string>> data;
this->sReturn = SQLFetch(this->sHandle);
while (this->sReturn == SQL_SUCCESS) {
vector<string> tuple;
for (int i = 0; i < columnCount; i++) {
SQLPOINTER value = "";
switch (info[i].columnType) {
case 0 : //SQL_UNKNOWN_TYPE
throw DatabaseAttributeTypeUnknown("The database returned an attribute of an unknown type.");
break;
case 1 : //SQL_CHAR
this->sReturn = SQLGetData(this->sHandle, i + 1, info[i].columnType, value,
info[i].columnSize*sizeof(SQLCHAR),
ERROR &length);
break;
//Some more cases
}
}
Any idea on why this error is being thrown? Here is the MSDN documentation on SQLGetData(), which is assigning a value to length.
Thank you for your time.
When a compiler maps executable code to line of code in the sources, they generally cannot differentiate lines in a statement that is split into multiple lines. So if the debugger says that an error happens on a specific line, it can actually be anywhere in the entire statement, so somewhere in:
The only shaky pointer here is
valuewhich point to a null static string, so to a one character long array containing a null byte. Also, depending on compiler options, this array can be in a read-only segment of data. While SQLGetData() thinks it points to a location which is at leastinfo[i].columnSize*sizeof(SQLCHAR)bytes in size and where it will write (not read) the data from the SQL column.I may miss other details, but my first guess is that is what causes the memory access violation.