shangqingLin / poly2tri

Automatically exported from code.google.com/p/poly2tri
Other
0 stars 0 forks source link

Freeing polyline point list? #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I can't find code that deletes the Points allocated for the vector<Point*> sent 
to the constructor CDT::CDT(). 

If we are supposed to manage this memory ourselves (which is logical), the 
testbed also does not free the data at the end of the program. 

Original issue reported on code.google.com by stevenlu...@gmail.com on 14 Dec 2011 at 7:29

GoogleCodeExporter commented 9 years ago
I believe the STD vector collecton automatically frees the memory after 
execution. Is this not correct? Try running valgrind to look for memory leaks, 
to which I believe there are none at the moment...

Original comment by mason.gr...@gmail.com on 14 Dec 2011 at 3:01

GoogleCodeExporter commented 9 years ago
EDIT: Google is your friend; stackoverflow.com answers the same exact question.

STD::vector does destruct its elements when it goes out of scope. The problem 
here is that your elements are pointers to data type Point, not Point structs 
themselves. If you instead did something like this:

vector<A> vec;
vec.push_back(A(1));
vec.push_back(A(2));

...then things would work as you expect.

Original comment by mason.gr...@gmail.com on 14 Dec 2011 at 3:13

GoogleCodeExporter commented 9 years ago
a vector<Point> would indeed free the Point but a vector<Point *> (which is the 
CDT constructor argument) will not. It's just that I can't tell whether the 
library is meant to free it for you. 

I have been freeing my lists when I finish with them and I am not getting any 
double-free errors. 

Not a big deal, really. 

Thanks for your work on this algorithm. It's quite fast :)

Original comment by stevenlu...@gmail.com on 14 Dec 2011 at 9:31

GoogleCodeExporter commented 9 years ago
I added code to the testbed example to free the points. I believe this should 
be the user's responsibility, not poly2tri per se.

Thanks for pointing this out; improves the example code.

Original comment by mason.gr...@gmail.com on 4 Feb 2012 at 9:04

GoogleCodeExporter commented 9 years ago
Yup I'm glad you took a look at this a second time. It certainly is the user's 
responsibility to free the points, and the testbed is meant to demonstrate what 
the user is supposed to do. 

Thanks

Original comment by stevenlu...@gmail.com on 4 Feb 2012 at 9:19