Why you need to make sure you always initialize your variables...
by Chris Cain · in General Discussion · 01/22/2009 (11:08 pm) · 2 replies
Well guys, I can't believe it... I made a big no-no, and I feel absolutely embarrassed. However, I feel that everyone can learn a bit from my mistake and I'll let you guys know what kind of interesting issues you can run into by failing to initialize a variable in C++.
I was working on some vectors for controlling my player, and it would do weird things (such as applying a down vector + an up vector resulted in an overall up momentum). Running in debug mode made the player be pushed to the south, randomly. In an attempt to solve the issue without debug mode, I started outputting my C++ vectors into the console, which caused crashes (further exploration in console.log showed me that the vectors became infinite length randomly, without explanation... an oddity in behaviour that would not happen without the output to the console ... and it was repeatable). I finally decided that the problem was almost definitely memory corruption, so I dove into many different functions looking for any opportunity for the Torque engine to corrupt the memory on accident... and then, right when I was about to give up, I saw it.
I was like "!!!!!!!!!!!!!!!!!!!!".
Changed to:
and 100% of issues were instantly fixed.
This goes to show you, guys... if you are having extrodinarily weird issues that make you think that ghosts in the machine, aliens, or the CIA are messing with you... make sure you initialized all of your variables properly.
Now I'm going to bed. :-p
I was working on some vectors for controlling my player, and it would do weird things (such as applying a down vector + an up vector resulted in an overall up momentum). Running in debug mode made the player be pushed to the south, randomly. In an attempt to solve the issue without debug mode, I started outputting my C++ vectors into the console, which caused crashes (further exploration in console.log showed me that the vectors became infinite length randomly, without explanation... an oddity in behaviour that would not happen without the output to the console ... and it was repeatable). I finally decided that the problem was almost definitely memory corruption, so I dove into many different functions looking for any opportunity for the Torque engine to corrupt the memory on accident... and then, right when I was about to give up, I saw it.
... Vector3 totalVector; totalVector += frontVector; ...
I was like "!!!!!!!!!!!!!!!!!!!!".
Changed to:
Vector3 totalVector(0.0f,0.0f,0.0f);
and 100% of issues were instantly fixed.
This goes to show you, guys... if you are having extrodinarily weird issues that make you think that ghosts in the machine, aliens, or the CIA are messing with you... make sure you initialized all of your variables properly.
Now I'm going to bed. :-p
#2
01/26/2009 (9:29 am)
I hate arrays/vectors... I always seem to run into problems when I use them, and it sometimes just makes the code longer/harder to read.
Torque 3D Owner Caylo Gypsyblood
Good stuff to know!