mercredi 30 novembre 2016

Bizzare nullref in C++

I have a small piece of C++ code that is making me insane. Whenever it runs, it throws a null reference exception in a very unexpected place.

void CSoundHandle::SetTarget(CSound* sound)
{
  assert(_Target == nullptr);
  if (sound == nullptr) { return; }

  _Target = sound;

  // This is the code that throws the exception.  It doesn't seem possible, as
  // we should not be able to get here if 'sound' is null.
  _Target->Stop();
}

So what the heck is going on? The message in the output window is:

this->_Target-> was nullptr.
0xC0000005: Access violation reading location 0x00000014

I have confirmed in disassembly that it is not taking place inside of the Stop function as well. How is this possible?

Aucun commentaire:

Enregistrer un commentaire