sfabel
January 23rd, 2008, 01:35 AM
Hi,
I've got a beginner's question regarding a memory leak that I am experiencing.
I've got this code which creates it:
AGradient *ag;
while(k < 50) {
k++;
ag = agradient(tmpimage);
// [then do something with it...]
}
AGradient is my own data structure (struct) defined in my own little library.
My funktion "agradient" calculates stuff, then returns a pointer to that structure, just like defined above. The problem I had was this: when I compiled my library, it gave a warning that I was returning a local pointer address, which I could only resolve by making the return variable global, and then returning its address. I don't know if I am going to burn in seventh hell for doing this, but I couldn't find another way.
In any case, the above code creates a ramp in memory usage, so it is a memory leak. I just don't know how to fix it. I read about new and delete, but I am not sure how to implement it. If I do this:
AGradient *ag;
ag = new AGradient;
And try to delete it before jumping into the next iteration of the while loop, it gives me an error (invalid pointer) and dumps core.
I don't really know what I am doing here, as you probably can tell and would be very glad for any pointers. I have read several tutorials and webpages but fail to see how it relates to my problem.
Thanks for any help or pointers,
Stephan
I've got a beginner's question regarding a memory leak that I am experiencing.
I've got this code which creates it:
AGradient *ag;
while(k < 50) {
k++;
ag = agradient(tmpimage);
// [then do something with it...]
}
AGradient is my own data structure (struct) defined in my own little library.
My funktion "agradient" calculates stuff, then returns a pointer to that structure, just like defined above. The problem I had was this: when I compiled my library, it gave a warning that I was returning a local pointer address, which I could only resolve by making the return variable global, and then returning its address. I don't know if I am going to burn in seventh hell for doing this, but I couldn't find another way.
In any case, the above code creates a ramp in memory usage, so it is a memory leak. I just don't know how to fix it. I read about new and delete, but I am not sure how to implement it. If I do this:
AGradient *ag;
ag = new AGradient;
And try to delete it before jumping into the next iteration of the while loop, it gives me an error (invalid pointer) and dumps core.
I don't really know what I am doing here, as you probably can tell and would be very glad for any pointers. I have read several tutorials and webpages but fail to see how it relates to my problem.
Thanks for any help or pointers,
Stephan