Ok, got a weird problem, wanna see if any of you can help figure it out.
I have a sparkle particle effect I've designed, but something isn't working with deallocating all the memory. It uses a dynamic array of Sparks. I currently have a menu and game module, and the weird thing is if I start, go back to the menu and start again it's fine, but then if i go back to menu a 2nd time and try to start, it just closes. I've debugged and found the problem to be with the Spark itself, meaning if I just have one Spark, and not even a sparkle it will do it.
class Spark {
private:
float xDir; // x direction
float yDir; // y direction
float speed; // speed of spark
float R; // red amount (0 - 1)
float G; // green amount (0 - 1)
float B; // blue amount (0 - 1)
public:
float x; // x location
float y; // y location
Spark();
void update(const float elapsedTime, const float radius); // w/ elapsed time & radius limit
void draw(float origx, float origy) const; // draw starting at origin x and y
};
Spark::Spark() : x(0.0f), y(0.0f) {
xDir = 1.0f/(100.0f-(float(rand()%199+1))); // random x dir between -1 and 1
yDir = 1.0f/(100.0f-(float(rand()%199+1))); // random y dir between -1 and 1
speed = float(rand()%9000+1000); // random speed between 1000 and 10,000
// pick 2 random numbers, and ensure they're not pretty much 0.
float a,b,c;
do {
a = rand() / (float)RAND_MAX;
b = rand() / (float)RAND_MAX;
c = (a > b)?a:b;
} while (c > 0.1f);
// scale them such that the larger number scales to 1.0f
float scale = 1.0f / c;
a *= scale;
b *= scale;
c = (rand() / (float)RAND_MAX) * 0.5f; // pick third value, ensure it's dark
float rgb[3];
int idx = rand() % 3; // store the first value
rgb[idx] = a; // in any of R,G,B
int idx2 = ((rand() & 1) + idx) % 3; // store the second value
rgb[idx2] = b; // in any remaining channel
int idx3 = ((idx+1) ^ (idx2+1))-1; // store the last value
rgb[idx3] = c; // in the remaining channel
R = rgb[0];
G = rgb[1];
B = rgb[2];
}
But, there's no pointers or anything, so I don't really need to define a destructor to set variables to 0. Why is it doing this. I figured out how to fix it, by defining a pointer, rather than just a variable, calling new to create it and delete-ing it at cleanup, and it solves the problem.
So I figured instead, just use a double pointer in the Sparkle class, however when I try that, the new double pointer setup gives me problems.
Sparkle::Sparkle(Vector2D location, float radius, int numParticles) {
this->location = location;
this->radius = radius;
this->numParticles = numParticles;
sparks = new Spark*[numParticles];
for (int i=0;i<numParticles;i++) {
sparks = new Spark();
}
}
Sparkle::~Sparkle() {
if (sparks) {
for (int i=0;i<numParticles;i++) {
delete sparks;
sparks = 0;
}
delete [] sparks;
}
sparks = 0;
}
Now it just does the same thing. Am I not properly deleting all the memory? Why is it doing it with the Spark, which has no pointers? Any help, suggestions would be appreciated