Crash bei getString und getInt
-
Meine Methode erzeugt beim Starten der Anwendung einen Fehler.
Fehlermeldung: Unbehandelte Ausnahme bei 0x60d657aa (msvcr100d.dll) in GameScope.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x000003da.Wenn ich
highScore.name[i] = res->getString("NAME"); highScore.score[i] = res->getInt("SCORE");
auskommentiereläuft die Anwendung fehlerfrei.
bool SQLConnection::getHighScore(GameTyp gameTyp){ LOG("getHighScore Start"); sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; sql::PreparedStatement *pstmt; std::string query; HighScore highScore; try{ //Establish connection to MySQL Server driver = get_driver_instance(); con = driver->connect("*******", "****", "*****"); ////Select gameScopeSQL Database on Server con->setSchema("gameScopeSQL"); ////Prepare and Execute Query to get 10 heighest scores with names pstmt = con->prepareStatement("SELECT NAME, SCORE FROM HIGHSCORE_SPACEINVADERS ORDER BY SCORE DESC LIMIT 10"); res = pstmt->executeQuery(); // Fetch in Highscore res->afterLast(); int i=0; while (res->previous()){ highScore.name[i] = res->getString("NAME"); highScore.score[i] = res->getInt("SCORE"); i++; LOG("Highscore"); // LOG(highScore.name[i]); // LOG(highScore.score[i]); } delete res; delete pstmt; } catch (sql::SQLException &e) { /* The MySQL Connector/C++ throws three different exceptions: - sql::MethodNotImplementedException (derived from sql::SQLException) - sql::InvalidArgumentException (derived from sql::SQLException) - sql::SQLException (derived from std::runtime_error) */ LOG("SQL FEHLER BEI GEHT HIGHSOCRE"); std::cout << "# ERR: SQLException in " << __FILE__; std::cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl; /* Use what() (derived from std::runtime_error) to fetch the error message */ std::cout << "# ERR: " << e.what(); std::cout << " (MySQL error code: " << e.getErrorCode(); std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl; std::cin.get(); // return false; } // return true; }
-
könnte mein Struct schuld sein?
Es liegt in der Headerdatei der Klasse.
struct HighScore { std::string name[9]; int score[9]; };
-
wenn ich einen std:string in der Methode erzeuge und diesen
mithighScoreString = res->getString("NAME").c_str();
zuweise funktioniert es.
-
Klassischer Off-by-one-Fehler.
9 Plaetze in deiner struct, aber 10 Ergebnisse aus der Datenbank:
pstmt = con->prepareStatement("SELECT NAME, SCORE FROM HIGHSCORE_SPACEINVADERS ORDER BY SCORE DESC LIMIT 10")
struct HighScore { std::string name[9]; int score[9]; };
-
Uebrigens schreit das foermlich nach einem Standardlibrary-Container.
Zb. ein
std::pair<std::string, int>
und daraus dann einstd::vector
oder so.
-
der Fehler lag im falschen String Format.