3

I'm checking my code for memory leaks. Everything is okay until I got the code:

mSystem = new LightSystem();
sf::View *view = th::DisplayManager::Get()->GetCamera();
mSystem->SetView(*view);

SetView does really tiny job (extracts a few members of passed view pointer. When latest code line is commented everything is okay, but uncommenting everything works in default mode and fails in memory leak detection with 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.

The questions are: why does it works successfully without valgrind and breaks with it. I have also tried to set --main-stacksize= a big value but it didn't help me.

Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • 3
    It probably doesn't "work" without valgrind. It's getting undefined behavior because you're using an invalid pointer, meaning that anything can happen. – Charles Salvia Apr 04 '12 at 12:19
  • Don't forget that the problem doesn't have to be related to the `mSystem->SetView(*view);` line. You might have corrupted anywhere in the memory anytime before that point. Valgrind might just be changing where things are... – enobayram Apr 04 '12 at 12:20

1 Answers1

4
==23703== Process terminating with default action of signal 11 (SIGSEGV)
==23703==  Access not within mapped region at address 0x8

At some point (probably LightSystem.cpp:55) you're dereferencing a pointer to which you assigned 8 which looks nothing like a valid address.

cnicutar
  • 178,505
  • 25
  • 365
  • 392