This should normally be part of any proper C++ training and we also have listed a few points in the wiki. But as a reminder I will list again some of the more important points. Please pay more attention to them, because these kinds of mistakes can be costly. We were lucky so far that nothing major bad has resulted from them.
- Every new needs to be matched by a delete.
- Do not use new[]/delete[]. Use a vector instead.
- Use new when it is needed. Common reasons are polymorphism and the need for more control about the life time of an object. If you don't need new, then avoid it. Just declaring a non-pointer member variable or local variable works fine often enough.
- Remember the rule of three (destructor, copy-constructor, assignment operator). If you have one off them, most likely you will need all three (if desired so, copy-constructors and assignment operators can be disabled instead, by declaring them private and not implementing them).
- RAII is mandatory. Classes with default constructor will initialise fine on there own (except when it is Ogre::Vector3 :( ). Most other stuff won't. If you a declare a build-in type member variable (like an int or a pointer) you need to initialise it in every single constructor of that class (usually in the initialiser list).
- Same goes for local variables. A line like
Code: Select all
int x;
Just reading the line above should ring the alarm bells for every seasoned C++ coder.