我正在检查我的代码是否有内存泄漏。一切都没问题,直到我拿到代码:
mSystem = new LightSystem();
sf::View *view = th::DisplayManager::Get()->GetCamera();
mSystem->SetView(*view);
SetView
做的工作非常小(提取了一些传递view
指针的成员。当最新的代码行被注释时,一切都没问题,但是取消注释一切都在默认模式下运行并且内存泄漏检测失败valgrind(valgrind --tool=memcheck ./Binary
)。
==23703== Use of uninitialised value of size 8
==23703== at 0x6B8472: ltbl::LightSystem::SetView(sf::View const&) (LightSystem.cpp:55)
==23703== by 0x6A7A7D: th::LightManager::Initialize() (LightManager.cpp:46)
==23703== by 0x6A75EA: th::Root::Initialize() (Root.cpp:101)
==23703== by 0x6A7113: th::Root::Root() (Root.cpp:66)
==23703== by 0x6A7553: th::Root::Get() (Root.cpp:88)
==23703== by 0x6291A8: th::Game::Initialize() (Game.cpp:36)
==23703== by 0x61DC1C: main (main.cpp:82)
==23703==
==23703== Invalid read of size 8
==23703== at 0x6B8472: ltbl::LightSystem::SetView(sf::View const&) (LightSystem.cpp:55)
==23703== by 0x6A7A7D: th::LightManager::Initialize() (LightManager.cpp:46)
==23703== by 0x6A75EA: th::Root::Initialize() (Root.cpp:101)
==23703== by 0x6A7113: th::Root::Root() (Root.cpp:66)
==23703== by 0x6A7553: th::Root::Get() (Root.cpp:88)
==23703== by 0x6291A8: th::Game::Initialize() (Game.cpp:36)
==23703== by 0x61DC1C: main (main.cpp:82)
==23703== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==23703==
==23703==
==23703== Process terminating with default action of signal 11 (SIGSEGV)
==23703== Access not within mapped region at address 0x8
==23703== at 0x6B8472: ltbl::LightSystem::SetView(sf::View const&) (LightSystem.cpp:55)
==23703== by 0x6A7A7D: th::LightManager::Initialize() (LightManager.cpp:46)
==23703== by 0x6A75EA: th::Root::Initialize() (Root.cpp:101)
==23703== by 0x6A7113: th::Root::Root() (Root.cpp:66)
==23703== by 0x6A7553: th::Root::Get() (Root.cpp:88)
==23703== by 0x6291A8: th::Game::Initialize() (Game.cpp:36)
==23703== by 0x61DC1C: main (main.cpp:82)
==23703== If you believe this happened as a result of a stack
==23703== overflow in your program's main thread (unlikely but
==23703== possible), you can try to increase the size of the
==23703== main thread stack using the --main-stacksize= flag.
==23703== The main thread stack size used in this run was 8388608.
问题是:为什么没有valgrind就可以成功运行并打破它。
我也尝试设置--main-stacksize=
一个很大的值,但它对我没有帮助。
答案 0 :(得分:4)
==23703== Process terminating with default action of signal 11 (SIGSEGV) ==23703== Access not within mapped region at address 0x8
在某些时候(可能是LightSystem.cpp:55
),您将取消引用指向8
的指针,该指针看起来不像有效地址。