我正在用C++和SFML做一个项目,用gcc版本6.3.0编译(MinGW.org GCC-6.3.0-1),一个地下城爬虫游戏。我试图通过房间的二维矢量创建布局(我也做了一个类,头是Room::Room(std::string layout)
),布局是通过一个种子创建的,该种子建立了任何房间的特征,这是用斜线符号(/)按房间分隔的
我首先阅读种子,直到它找到斜杠,然后创建一个Room示例并将其添加到向量中
/* Get Level Layout */
layout = getLayout(level); //Calls SQLite DB
layout_dim = getLayoutDim(level); //Calls SQLite DB
rows = layout_dim[0];
cols = layout_dim[1];
std::vector<std::vector<Room*>> layout_2d(rows);
std::string acumul = "";
int current_row = 0;
int current_col = 0;
for (size_t i = 0; i < strlen(layout.c_str()); i++){
if(layout[i] == '/'){
Room* r = new Room(acumul);
layout_2d[current_row][current_col] = r;
if(current_col + 1 == cols){ //That was last column on the row
current_row ++;
current_col = 0;
}
else{
current_col ++;
}
acumul = "";
}
else{
acumul = acumul + layout[i];
}
}
当它开始创建示例时,(Room* r = new Room(acumul);
)一切按预期进行,房间“0 1001 0 0”的“种子”正常
npos:4294967295
_M_dataplus
std::allocator<char> (base):
std::allocator<char>
_M_p:
0x8bdd98 "0 1001 0 0"
_M_string_length: 10
但是当它进入类Room的构造函数时,就变成了这个
npos: 4294967295
_M_dataplus
std::allocator<char> (base):
std::allocator<char>
_M_p:
0x8bddd0 "�"
_M_string_length: 9166224
查看VS Code附带的十六进制编辑器,看起来指针已经移动:这是正常时x1c 0d1x
当NOK
如果你需要更多的信息,请问我,并提前感谢
EDIT:当我启动属性-fsanitize=address,undefined -Wall -Wextra
时,有如下跟踪
g++ main.cpp sMenu.cpp sSettings.cpp sGame.cpp Room.cpp ConfigFile.cpp common.cpp db.cpp sqlite3.o -fdiagnostics-color=always -fsanitize=address,undefined -Wall -Wextra -o C:\Users\ricc_\Documents\Proyectos\Development\madou4cplusplus\src\..\dist\madou4.exe -L..\lib -I..\include -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system -g
sGame.cpp: In member function 'int sGame::getDataFromSeed(int)':
sGame.cpp:32:32: warning: unused parameter 'seed' [-Wunused-parameter]
int sGame::getDataFromSeed(int seed){
^~~~
sGame.cpp: In member function 'virtual std::__cxx11::string sGame::Run(sf::RenderWindow&)':
sGame.cpp:72:19: warning: unused variable 'r' [-Wunused-variable]
Room* r = new Room(acumul);
^
common.cpp: In function 'std::__cxx11::wstring FromUTF8(const char*)':
common.cpp:69:61: warning: comparison is always true due to limited range of data type [-Wtype-limits]
else if(std::numeric_limits<wchar_t>::max() < 0x110000){
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
db.cpp: In function 'int emptyCallback(void*, int, char**, char**)':
db.cpp:7:32: warning: unused parameter 'data' [-Wunused-parameter]
static int emptyCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:7:42: warning: unused parameter 'argc' [-Wunused-parameter]
static int emptyCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:7:55: warning: unused parameter 'argv' [-Wunused-parameter]
static int emptyCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:7:68: warning: unused parameter 'azColName' [-Wunused-parameter]
static int emptyCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~
db.cpp: In function 'int langGetCallback(void*, int, char**, char**)':
db.cpp:16:34: warning: unused parameter 'data' [-Wunused-parameter]
static int langGetCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:16:70: warning: unused parameter 'azColName' [-Wunused-parameter]
static int langGetCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~
db.cpp: In function 'std::__cxx11::string* langGet(std::__cxx11::string, int)':
db.cpp:33:9: warning: unused variable 'rc' [-Wunused-variable]
int rc = sqlite3_exec(DB, sql.c_str(), langGetCallback, (void*)"", NULL);
^~
db.cpp: In function 'int configSingleGetCallback(void*, int, char**, char**)':
db.cpp:42:42: warning: unused parameter 'data' [-Wunused-parameter]
static int configSingleGetCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:42:78: warning: unused parameter 'azColName' [-Wunused-parameter]
static int configSingleGetCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~
db.cpp: In function 'std::__cxx11::string configSingleGet(std::__cxx11::string)':
db.cpp:57:9: warning: unused variable 'rc' [-Wunused-variable]
int rc = sqlite3_exec(DB, sql.c_str(), configSingleGetCallback, (void*)"", NULL);
^~
db.cpp: In function 'int getLayoutCallback(void*, int, char**, char**)':
db.cpp:66:29: warning: unused parameter 'data' [-Wunused-parameter]
int getLayoutCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:66:65: warning: unused parameter 'azColName' [-Wunused-parameter]
int getLayoutCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~
db.cpp: In function 'std::__cxx11::string getLayout(int)':
db.cpp:81:9: warning: unused variable 'rc' [-Wunused-variable]
int rc = sqlite3_exec(DB, sql.c_str(), getLayoutCallback, (void*)"", NULL);
^~
db.cpp: In function 'int getLayoutDimCallback(void*, int, char**, char**)':
db.cpp:90:32: warning: unused parameter 'data' [-Wunused-parameter]
int getLayoutDimCallback(void* data, int argc, char** argv, char** azColName){
^~~~
db.cpp:90:68: warning: unused parameter 'azColName' [-Wunused-parameter]
int getLayoutDimCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~
db.cpp: In function 'int* getLayoutDim(int)':
db.cpp:105:9: warning: unused variable 'rc' [-Wunused-variable]
int rc = sqlite3_exec(DB, sql.c_str(), getLayoutDimCallback, (void*)"", NULL);
^~
db.cpp: In function 'int configUpdate(std::__cxx11::string, std::__cxx11::string)':
db.cpp:120:9: warning: unused variable 'rc' [-Wunused-variable]
int rc = sqlite3_exec(DB, sql.c_str(), emptyCallback, (void*)"", NULL);
^~
db.cpp: In function 'int langGetCallback(void*, int, char**, char**)':
db.cpp:16:12: internal compiler error: in pp_format, at pretty-print.c:630
static int langGetCallback(void* data, int argc, char** argv, char** azColName){
^~~~~~~~~~~~~~~
db.cpp:16:12: internal compiler error: Aborted
g++: internal compiler error: Aborted (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
在这一刻,我怀疑向量的问题有任何关系(虽然我很欣赏这一点),我试图删除所有的调用向量,二维数组等;构造函数仍然会破坏它(现在抛出一个值0x 8addd 0“�”)。
1条答案
按热度按时间8yoxcaq71#
看起来你的vector中有零大小的vector:
现在你可以这样排序:
但这又引出了另一个问题,你的内部类型是一个unmanged指针
Room*
,你可能在某个时候正确地删除了所有这些房间(你没有显示代码),但它可能不是exeption安全的。我会改变类型和你添加房间的方式,使一切都安全:
然后更改代码以将房间插入到: