質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
デバッグ

デバッグはプログラムのバグや欠陥を検知し、開発中のバグを取り除く為のプロセスを指します。

Q&A

2回答

6298閲覧

gdbデバッガによる「Cannot access memory at address」の対処方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

デバッグ

デバッグはプログラムのバグや欠陥を検知し、開発中のバグを取り除く為のプロセスを指します。

0グッド

0クリップ

投稿2022/02/04 03:45

編集2022/02/04 07:12

提示コードですがgdbデバッガにて///コメント部内部の「Cannot access memory at address」エラーが発生するのですがこれはどう対処したらいいのでしょうか?下記の提示コードが問題の関数ですが問題点がわかりません。

Console
(gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/shigurechan/prg/TopDownGame/Game [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7fffec8fb700 (LWP 25028)] [New Thread 0x7fffe7fff700 (LWP 25029)] Thread 1 "Game" received signal SIGSEGV, Segmentation fault. 0x00005555555653d1 in glm::vec<2, int, (glm::qualifier)0>::vec (this=0x5555555c8d82, _x=0, _y=0) at /usr/local/include/glm/detail/type_vec2.inl:40 40 : x(_x), y(_y) (gdb) bt #0 0x00005555555653d1 in glm::vec<2, int, (glm::qualifier)0>::vec (this=0x5555555c8d82, _x=0, _y=0) at /usr/local/include/glm/detail/type_vec2.inl:40 #1 0x0000555555589c48 in FrameWork::texture::texture (this=0x5555555c8d82) at FrameWork/source/../header/Resource_use.hpp:20 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #2 0x00005555555894f5 in FrameWork::LoadTexture (fileName=0x1 <error: Cannot access memory at address 0x1>) at FrameWork/source/Resource.cpp:288 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #3 0x000055555559e129 in Player::Player (this=0x55555565ac20) at source/Player.cpp:17 #4 0x000055555559d09b in __gnu_cxx::new_allocator<Player>::construct<Player> (this=0x7fffffffd9df, __p=0x55555565ac20) at /usr/include/c++/9/ext/new_allocator.h:147 #5 0x000055555559cd29 in std::allocator_traits<std::allocator<Player> >::construct<Player> (__a=..., __p=0x55555565ac20) at /usr/include/c++/9/bits/alloc_traits.h:484 #6 0x000055555559c7a4 in std::_Sp_counted_ptr_inplace<Player, std::allocator<Player>, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<>(std::allocator<Player>) (this=0x55555565ac10, __a=...) at /usr/include/c++/9/bits/shared_ptr_base.h:548 #7 0x000055555559c104 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<Player, std::allocator<Player>>(Player*&, std::_Sp_alloc_shared_tag<std::allocator<Player> >) (this=0x7fffffffdb08, __p=@0x7fffffffdb00: 0x0, __a=...) at /usr/include/c++/9/bits/shared_ptr_base.h:679 #8 0x000055555559bd78 in std::__shared_ptr<Player, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<Player>>(std::_Sp_alloc_shared_tag<std::allocator<Player> >) (this=0x7fffffffdb00, __tag=...) at /usr/include/c++/9/bits/shared_ptr_base.h:1344 #9 0x000055555559ba69 in std::shared_ptr<Player>::shared_ptr<std::allocator<Player>>(std::_Sp_alloc_shared_tag<std::allocator<Player> >) (this=0x7fffffffdb00, __tag=...) at /usr/include/c++/9/bits/shared_ptr.h:359 #10 0x000055555559b740 in std::allocate_shared<Player, std::allocator<Player>>(std::allocator<Player> const&) (__a=...) at /usr/include/c++/9/bits/shared_ptr.h:702 #11 0x000055555559b3b0 in std::make_shared<Player> () at /usr/include/c++/9/bits/shared_ptr.h:718 #12 0x000055555559add7 in Game::Game (this=0x555555e96d60) at source/Game.cpp:8 #13 0x000055555559aab3 in __gnu_cxx::new_allocator<Game>::construct<Game> (this=0x7fffffffdc1f, __p=0x555555e96d60) at /usr/include/c++/9/ext/new_allocator.h:147 #14 0x000055555559a9b3 in std::allocator_traits<std::allocator<Game> >::construct<Game> (__a=..., __p=0x555555e96d60) at /usr/include/c++/9/bits/alloc_traits.h:484 #15 0x000055555559a7f2 in std::_Sp_counted_ptr_inplace<Game, std::allocator<Game>, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<>(std::allocator<Game>) (this=0x555555e96d50, __a=...) at /usr/include/c++/9/bits/shared_ptr_base.h:548 #16 0x000055555559a536 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<Game, std::allocator<Game>>(Game*&, std::_Sp_alloc_shared_tag<std::allocator<Game> >) (this=0x7fffffffdd38, __p=@0x7fffffffdd30: 0x0, __a=...) at /usr/include/c++/9/bits/shared_ptr_base.h:679 #17 0x000055555559a3fa in std::__shared_ptr<Game, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<Game>>(std::_Sp_alloc_shared_tag<std::allocator<Game> >) (this=0x7fffffffdd30, __tag=...) at /usr/include/c++/9/bits/shared_ptr_base.h:1344 #18 0x000055555559a2f3 in std::shared_ptr<Game>::shared_ptr<std::allocator<Game>>(std::_Sp_alloc_shared_tag<std::allocator<Game> >) (this=0x7fffffffdd30, __tag=...) at /usr/include/c++/9/bits/shared_ptr.h:359 #19 0x000055555559a1fc in std::allocate_shared<Game, std::allocator<Game>>(std::allocator<Game> const&) (__a=...) at /usr/include/c++/9/bits/shared_ptr.h:702 #20 0x000055555559a0c2 in std::make_shared<Game> () at /usr/include/c++/9/bits/shared_ptr.h:718 #21 0x0000555555599ec2 in Entry::Entry (this=0x5555557c7990) at source/Entry.cpp:8 #22 0x000055555559db7b in std::make_unique<Entry> () at /usr/include/c++/9/bits/unique_ptr.h:857 --Type <RET> for more, q to quit, c to continue without paging--
FrameWork::LoadTexture()

cpp

1 2// ##################################### テクスチャをロード ##################################### 3FrameWork::Texture FrameWork::LoadTexture(const char* fileName) 4{ 5 Texture texture; 6 7 glm::ivec2 size; 8 int channel; 9 10 unsigned char* data = NULL; 11 data = stbi_load(fileName, &size.x, &size.y, &channel, 0); 12 13 texture.size = size; 14 15 if (data == NULL) 16 { 17 std::cerr << "画像が見つかりません: " << fileName << std::endl; 18 assert(0); 19 } 20 21 glGenTextures(1, &texture.ID); //テクスチャIDの生成 22 glBindTexture(GL_TEXTURE_2D, texture.ID); // IDバインド 23 24 //テクスチャ生成 25 26 if (channel == 4) 27 { 28 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 29 } 30 else if (channel == 3) 31 { 32 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 33 } 34 else if (channel == 2) 35 { 36 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size.x, size.y, 0, GL_RG, GL_UNSIGNED_BYTE, data); 37 } 38 else if (channel == 1) 39 { 40 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, size.x, size.y, 0, GL_RED, GL_UNSIGNED_BYTE, data); 41 } 42 else 43 { 44 std::cerr << "未対応の形式のチャンネル数です: " << fileName <<" "<< "チャンネル数: "<<channel<< std::endl; 45 assert(0); 46 } 47 48 // テクスチャの補間設定 49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 53 glGenerateMipmap(GL_TEXTURE_2D); 54 glBindTexture(GL_TEXTURE_2D, 0); 55 56 texture.data = data; 57 58 return texture; 59 60}
Player.cpp
#include "../header/Player.hpp" #define SPEED (int)4; #define WEAPON_ANIMATION_SPEED ((int)10) #define WALK_ANIMATION_SPEED ((int)20) #define THROW_SPEED ((int)5) #define THROW_MOVE_SPEED ((int)150) #define THROW_DISTANCE ((int)300) Player::Player() : Actor() { position = glm::ivec2(0,0); throwPosition = glm::ivec2(0,0); throwDistance = 0; speed = SPEED; moveVector = glm::vec2(1,0); up_sprite = FrameWork::LoadTexture("Asset/texture/up.png"); down_sprite = FrameWork::LoadTexture("Asset/texture/down.png"); left_sprite = FrameWork::LoadTexture("Asset/texture/left.png"); right_sprite = FrameWork::LoadTexture("Asset/texture/right.png"); sword_right_sprite[0] = FrameWork::LoadTexture("Asset/texture/sword_right_0.png"); sword_right_sprite[1] = FrameWork::LoadTexture("Asset/texture/sword_right_1.png"); sword_right_sprite[2] = FrameWork::LoadTexture("Asset/texture/sword_right_2.png"); sword_right_sprite[3] = FrameWork::LoadTexture("Asset/texture/sword_right_3.png"); isThrow = false; state = State::Wait; animation_Attack_Time = 0; animation_Attack_Clip = 1; animation_Time = 0; animation = false; isMove = false; isAttack = false; }
FrameWork::Texture 構造体

cpp

1 //テクスチャファイル 2 typedef struct texture 3 { 4 glm::ivec2 size = glm::ivec2(0,0); //サイズ 5 GLuint ID = 0; //テクスチャハンドル 6 unsigned char* data; //ピクセルデータ 7 } 8 Texture; 9
Makefile
PRG :=Game SRC_DIR :=FrameWork/source SRC_USE_DIR :=source OBJ_DIR :=FrameWork/object OBJ_USE_DIR :=object DEP_DIR :=FrameWork/object DEP :=$(wildcard $(DEP_DIR)/*.d) SRC :=$(wildcard $(SRC_DIR)/*.cpp) SRC_USE :=$(wildcard $(SRC_USE_DIR)/*.cpp) OBJ :=$(addprefix $(OBJ_DIR)/,$(patsubst %.cpp,%.o,$(notdir $(SRC)))) OBJ_USE :=$(addprefix $(OBJ_USE_DIR)/,$(patsubst %.cpp,%.o,$(notdir $(SRC_USE)))) $(PRG): $(OBJ) $(OBJ_USE) $(CXX) $^ -o $@ -ldl -lGLESv2 -lGLU -lGL -lglfw3 -pthread -lGL -lGLEW -lfreetype -ldl -lX11 $(OBJ_DIR)/%.o: FrameWork/source/%.cpp $(CXX) -g -c -MMD -MP $< -o $@ -include $(DEP) $(OBJ_USE_DIR)/%.o: source/%.cpp $(CXX) -g -c -MMD -MP $< -o $@ -include $(DEP) clean: rm -f ./$(OBJ_USE_DIR)/*.o ./$(OBJ_DIR)/*.o *.out ./$(OBJ_DIR)/*.d ./$(OBJ_USE_DIR)/*.d $(PRG)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

#2 0x00005555555894f5 in FrameWork::LoadTexture (fileName=0x1 <error: Cannot access memory at address 0x1>) at FrameWork/source/Resource.cpp:288

gdbがスタックを表示するとき、関数の引数の値をできるだけ表示しようとします。LoadTexture()fileName 引数は const char* なので、おそらく gdb は fileName が指す文字列を表示したいのですが、fileName の値が 0x1 という異常な値なので指す先のメモリを読み取れない、ということです。

投稿2022/02/04 03:52

int32_t

総合スコア20832

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/02/04 04:10

なるほど。つまり何が原因なのでしょうか?
int32_t

2022/02/04 04:28

じゅうぶん書いてあると思うのですけど… > fileName の値が 0x1 という異常な値なので Player.cpp の17行目あたりでおかしな引数をLoadTexture()に渡している可能性が高いです。 それか、どこかでスタック破壊してるか。
退会済みユーザー

退会済みユーザー

2022/02/04 05:47

はい。確認したのですが何もおかしな点がありません。ファイルが読み込めない場合はエラーが出るのですが 何がおかしいのでしょうか?
int32_t

2022/02/04 05:52

たしかに Player.cpp はおかしくなさそうですね。 あとは Texture クラスのコンストラクタがスタック破壊している可能性ですかね。
退会済みユーザー

退会済みユーザー

2022/02/04 05:55

はい、提示コードの下部に texture構造体の定義を追加しました。 はやりこの構造体も問題がないと思うのですがこれは何が悪いのでしょうか?
int32_t

2022/02/04 06:49

うーん、Texture にも問題なさそうですね。 あと考えられるのはスタック不足ぐらいでしょうか。bt で出てくる関数の中で、大きなローカル変数を使ってたりしませんか? Linux だったら、ulimit コマンドでスタックサイズを増やすと直ったりしませんか?
退会済みユーザー

退会済みユーザー

2022/02/04 07:00

make cleanしたら動きました。また最近なんだからセグメンテーションエラーが起きたり起きなかったりと動作が不安定なのですがこれは何が関係しているのでしょうか?
guest

0

ベストアンサー

make cleanコマンドを実行してビルドし直したら治った。

clean: rm -f ./$(OBJ_USE_DIR)/*.o ./$(OBJ_DIR)/*.o *.out ./$(OBJ_DIR)/*.d ./$(OBJ_USE_DIR)/*.d $(PRG)

投稿2022/02/04 07:01

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問