OK, as someone clearly spoiled by the long use of managed languages, I’m looking for some pointers (pun intended) on best practices for creating and managing class instances.
Here’s a simple example of what I’m doing now, which results in the eventual crashing of my program (or that’s what it looks like, anyway):
class MyGameObject {
public:
MyGameObject();
int x;
int y;
};
MyGameObject::MyGameObject(){
this->x = rand() % 160;
this->y = rand() % 128;
}
class MyGame {
// private vars
DisplayN18 disp;
vector<MyGameObject *> objs;
static const int MAX_OBJS = 20;
// private functions
void init();
// public interfaces
public:
MyGame();
void tick();
};
MyGame::MyGame(){
this->init();
}
void MyGame::init() {
disp.clear();
MyGameObject *obj;
this->disp.clear();
for (int i = 0; i < MyGame::MAX_OBJS; i++) {
obj = new MyGameObject;
objs.push_back(obj);
disp.fillRect(obj->x, obj->y, 2, 2, DisplayN18::WHITE);
wait_ms(50);
}
}
void MyGame::tick() {
MyGameObject *obj;
obj = new MyGameObject;
// erase oldest object by drawing over it with black
disp.fillRect(objs.front()->x, objs.front()->y, 2, 2, DisplayN18::BLACK);
// remove oldest object from vector
objs.erase(objs.begin());
// add new object to vector
objs.push_back(obj);
// draw new object
disp.fillRect(obj->x, obj->y, 2, 2, DisplayN18::WHITE);
wait_ms(10);
}
int main() {
MyGame game;
while (true)
game.tick();
}
What I’m trying to figure out is whether the technique(s) I’m currently using are apt to lead to memory leaks, and if so, how to correct them. Or perhaps I’m having issues because of how I’m drawing items on the display?
The symptoms I’ve seen are:
[ul]In some cases (usually if I’m including text on screen for debugging), the display stops updating, and I get a small rectangle made up of purple and green lines in the upper left corner of the display. When using a build that does this, it stops updating consistently in the same spot on every run.
In other cases, the objects continue to be created and erased for a while, and at some point, no new objects are created (or if they’re created, they’re not drawn on the display) but the old ones are erased, until the display is completely blank. Short of adding USBSerial for debugging (which I may not even have enough room for), I have no way to tell if the program is still running at this point (though I suppose I could set up one of the LEDs to blink on/off every tick as a means of testing that).[/ul]
As I’ve noted elsewhere, I’m very new to C++, and much more used to C#, VB, and JavaScript, so I’m faced with trying to be productive with a very sharp knife that takes time to learn. Any advice or recommendations are welcome.
EDIT: I should note that I arrived at the code above through trial and error, working from the example program that ships with RETRO as a starting point, looking up unfamiliar stuff on http://www.cplusplus.com/ and the mbed site, and fixing compiler errors as I find them. So it’s entirely possible that the code above, while it may compile, may be complete crap. Feel free to say so…my feelings won’t be hurt.