提示コードなのですが画像を読み込むコードで以下のコンソール画面の実行時エラーが発生します。なぜinvalid size
になるのでしょうか?prite = FrameWork::LoadTexture("asset/texture/brickChip.png");
をコメントアウトすると実行できるためこの関数が原因なのでは事実です。メモリ確保エラーのメッセージと思われます。
確認したこと
逐次実行でFrameWork::LoadTexture()関数が原因であることが判明 ※コメントアウトして実行して確認
画像ファイルが読み込まれていること。画像ファイルが存在していることを確認
参考サイトAより
日本語訳
エラーを検索すると、Cプログラミングでよくあるエラーであることがわかります。プログラムが割り当てられたメモリ(文字列や動的配列、または自分で割り当てたもの)で範囲外になっていることを意味しているようです。(ただし、-exxがキャッチしない場合は、おそらく配列ではありません。) gdbなどのデバッガーで詳細を確認できるはずです。 -gを指定してコンパイルし、gdbにロードしてプログラムを「実行」すると、その時点で停止し、行番号が表示されるか、バックトレース(「bt」)してどの関数を確認できるかがわかります。コードが実行されていました。
とあるのですがbtコマンドでトレースしました。
LoadTexture()関数の//stbi_image_free(texture.data);のコメントアウトを解除してDeleteTexture() 関数の//stbi_image_free(texture.data);をコメントアウトするとdouble free or corruption (!prev)
というエラーが出ます。
参考サイトA:https://www.freebasic.net/forum/viewtopic.php?t=29372#:~:text=Re%3A%20malloc()%3A%20invalid%20size%20(unsorted)&text=It%20looks%20like%20it%20means,able%20to%20tell%20you%20more.
参考サイトB: https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error
コンソール画面
$ ./Game ああ ああ malloc(): invalid size (unsorted) 中止 (コアダンプ)
gdbデバッグ画面
(gdb) run Starting program: /home/shigurechan/prg/2D_GameProject/Game/Game [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7fffece88700 (LWP 37573)] [New Thread 0x7fffe7fff700 (LWP 37574)] ああ ああ malloc(): invalid size (unsorted) Thread 1 "Game" received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 50 ../sysdeps/unix/sysv/linux/raise.c: そのようなファイルやディレクトリはありません. (gdb) bt #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 #1 0x00007ffff79ab859 in __GI_abort () at abort.c:79 #2 0x00007ffff7a1629e in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7b40298 "%s\n") at ../sysdeps/posix/libc_fatal.c:155 #3 0x00007ffff7a1e32c in malloc_printerr (str=str@entry=0x7ffff7b42a50 "malloc(): invalid size (unsorted)") at malloc.c:5347 #4 0x00007ffff7a210e4 in _int_malloc (av=av@entry=0x7ffff7b75b80 <main_arena>, bytes=bytes@entry=10048) at malloc.c:3736 #5 0x00007ffff7a232c9 in __GI___libc_malloc (bytes=10048) at malloc.c:3066 #6 0x00007ffff7db4b39 in operator new(unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6 #7 0x0000555555578a97 in __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<Generate_Dungeon, std::allocator<Generate_Dungeon>, (__gnu_cxx::_Lock_policy)2> >::allocate (this=<optimized out>, __n=1) at /usr/include/c++/9/ext/new_allocator.h:102 #8 std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<Generate_Dungeon, std::allocator<Generate_Dungeon>, (__gnu_cxx::_Lock_policy)2> > >::allocate (__a=..., __n=1) at /usr/include/c++/9/bits/alloc_traits.h:443 #9 std::__allocate_guarded<std::allocator<std::_Sp_counted_ptr_inplace<Generate_Dungeon, std::allocator<Generate_Dungeon>, (__gnu_cxx::_Lock_policy)2> > > (__a=...) at /usr/include/c++/9/bits/allocated_ptr.h:97 #10 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<Generate_Dungeon, std::allocator<Generate_Dungeon>>(Generate_Dungeon*&, std::_Sp_alloc_shared_tag<std::allocator<Generate_Dungeon> >) (__a=..., __p=<optimized out>, this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:677 #11 std::__shared_ptr<Generate_Dungeon, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<Generate_Dungeon>>(std::_Sp_alloc_shared_tag<std::allocator<Generate_Dungeon> >) (__tag=..., this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:1344 #12 std::shared_ptr<Generate_Dungeon>::shared_ptr<std::allocator<Generate_Dungeon>>(std::_Sp_alloc_shared_tag<std::allocator<Generate_Dungeon> >) (__tag=..., this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr.h:359 #13 std::allocate_shared<Generate_Dungeon, std::allocator<Generate_Dungeon>>(std::allocator<Generate_Dungeon> const&) (__a=...) at /usr/include/c++/9/bits/shared_ptr.h:702 #14 std::make_shared<Generate_Dungeon> () at /usr/include/c++/9/bits/shared_ptr.h:718 #15 Stage::Stage (this=0x555555e72a10) at source/Stage.cpp:10 #16 0x000055555557729a in __gnu_cxx::new_allocator<Stage>::construct<Stage> (this=<optimized out>, __p=0x555555e72a10) at /usr/include/c++/9/new:174 #17 std::allocator_traits<std::allocator<Stage> >::construct<Stage> (__a=..., __p=0x555555e72a10) at /usr/include/c++/9/bits/alloc_traits.h:483 #18 std::_Sp_counted_ptr_inplace<Stage, std::allocator<Stage>, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<>(std::allocator<Stage>) (__a=..., this=0x555555e72a00) at /usr/include/c++/9/bits/shared_ptr_base.h:548 #19 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<Stage, std::allocator<Stage>>(Stage*&, std::_Sp_alloc_shared_tag<std::allocator<Stage> >) (__a=..., __p=<optimized out>, this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:679 #20 std::__shared_ptr<Stage, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<Stage>>(std::_Sp_alloc_shared_tag<std::allocator<Stage> >) (__tag=..., this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:1344 #21 std::shared_ptr<Stage>::shared_ptr<std::allocator<Stage>>(std::_Sp_alloc_shared_tag<std::allocator<Stage> >) ( __tag=..., this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr.h:359 #22 std::allocate_shared<Stage, std::allocator<Stage>>(std::allocator<Stage> const&) (__a=...) --Type <RET> for more, q to quit, c to continue without paging-- at /usr/include/c++/9/bits/shared_ptr.h:702 #23 std::make_shared<Stage> () at /usr/include/c++/9/bits/shared_ptr.h:718 #24 Game::Game (this=0x55555578d2c0) at source/Game.cpp:9 #25 0x0000555555576f47 in __gnu_cxx::new_allocator<Game>::construct<Game> (this=<optimized out>, __p=0x55555578d2c0) at /usr/include/c++/9/new:174 #26 std::allocator_traits<std::allocator<Game> >::construct<Game> (__a=..., __p=0x55555578d2c0) at /usr/include/c++/9/bits/alloc_traits.h:483 #27 std::_Sp_counted_ptr_inplace<Game, std::allocator<Game>, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<>(std::allocator<Game>) (__a=..., this=0x55555578d2b0) at /usr/include/c++/9/bits/shared_ptr_base.h:548 #28 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<Game, std::allocator<Game>>(Game*&, std::_Sp_alloc_shared_tag<std::allocator<Game> >) (__a=..., __p=<optimized out>, this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:679 #29 std::__shared_ptr<Game, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<Game>>(std::_Sp_alloc_shared_tag<std::allocator<Game> >) (__tag=..., this=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:1344 (gdb)
cpp
1Stage::Stage() 2{ 3 sprite = FrameWork::LoadTexture("asset/texture/brickChip.png"); 4 5 dungeon = std::make_shared<Generate_Dungeon>(); 6 dungeon->GetStage(grid); //ダンジョン生成 7} 8
cpp
1// ##################################### テクスチャをロード ##################################### 2FrameWork::Texture FrameWork::LoadTexture(const char* fileName) 3{ 4 Texture texture; 5 6 glm::ivec2 size; 7 int channel; 8 9 unsigned char* data = NULL; 10 data = stbi_load(fileName, &size.x, &size.y, &channel, 0); 11 texture.size = size; 12 13 if (data == NULL) 14 { 15 std::cerr << "画像が見つかりません: " << fileName << std::endl; 16 assert(0); 17 } 18 19 glGenTextures(1, &texture.ID); //テクスチャIDの生成 20 glBindTexture(GL_TEXTURE_2D, texture.ID); // IDバインド 21 22 //テクスチャ生成 23 24 if (channel == 4) 25 { 26 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 27 } 28 else if (channel == 3) 29 { 30 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 31 } 32 else if (channel == 2) 33 { 34 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size.x, size.y, 0, GL_RG, GL_UNSIGNED_BYTE, data); 35 } 36 else if (channel == 1) 37 { 38 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, size.x, size.y, 0, GL_RED, GL_UNSIGNED_BYTE, data); 39 } 40 else 41 { 42 std::cerr << "未対応の形式のチャンネル数です: " << fileName <<" "<< "チャンネル数: "<<channel<< std::endl; 43 assert(0); 44 } 45 46 // テクスチャの補間設定 47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 51 glBindTexture(GL_TEXTURE_2D, 0); 52 53 texture.data = data; 54 55 printf("ああ\n"); 56 57 58 //stbi_image_free(texture.data); 59 return texture; 60 61} 62 63 64 65// ##################################### テクスチャを削除 ##################################### 66void FrameWork::DeleteTexture(FrameWork::Texture texture) 67{ 68 glDeleteTextures(1,&texture.ID); 69 //stbi_image_free(texture.data); 70 //texture.data = NULL; 71} 72 73 74
Genrate_Dungeon.cpp
cpp
1 2Generate_Dungeon::Generate_Dungeon() 3{ 4 area.resize(0); 5 area.push_back(Area { 100 * 100,glm::ivec2(0,0),glm::ivec2(100,100) }); 6 7} 8/* 9文字数の関係で省略 10 11*/
こちらの質問が複数のユーザーから「調査したこと・試したことが記載されていない質問」という指摘を受けました。





あなたの回答
tips
プレビュー