Skip to content

Commit 49ab1a3

Browse files
dcwhiteallywarner
authored andcommitted
More memory leaks
1 parent 08422ac commit 49ab1a3

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

2DConvexHull.h

+15-19
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,39 @@
2424
//-------------------------------------------------------------------
2525
//-------------------------------------------------------------------
2626

27+
#ifndef TWODCONVEXHULL
28+
#define TWODCONVEXHULL 1
29+
2730
#include <iostream>
2831
#include <stack>
2932
#include <vector>
3033
#include <stdlib.h>
3134

3235
#include "vec3.h"
3336

34-
#ifndef TWODCONVEXHULL
35-
#define TWODCONVEXHULL 1
36-
37-
using std::vector;
38-
using std::stack;
39-
4037
struct Point
4138
{
4239
double x, y;
4340
int index;
4441
};
45-
42+
4643
// A utility function to find next to top in a stack
47-
Point nextToTop(stack<Point> &S)
44+
Point nextToTop(std::stack<Point> &S)
4845
{
4946
Point p = S.top();
5047
S.pop();
5148
Point res = S.top();
5249
S.push(p);
5350
return res;
5451
}
55-
52+
5653
void swap(Point &p1, Point &p2)
5754
{
5855
Point temp = p1;
5956
p1 = p2;
6057
p2 = temp;
6158
}
62-
59+
6360
double distSq(const Point& p1, const Point& p2)
6461
{
6562
return (p1.x - p2.x) * (p1.x - p2.x) +
@@ -101,8 +98,9 @@ int compare(const void *vp1, const void *vp2)
10198

10299
// Prints convex hull of a set of n points.
103100
// TODO: points gets overwritten...
104-
stack<Point> convexHull(Point points[], const int n)
101+
std::stack<Point> convexHull(std::vector<Point>& points)
105102
{
103+
const int n = points.size();
106104
// Find the bottommost point
107105
int ymin = points[0].y, min = 0;
108106
for (int i = 1; i < n; i++)
@@ -116,7 +114,7 @@ stack<Point> convexHull(Point points[], const int n)
116114
}
117115

118116
// Place the bottom-most point at first position
119-
swap(points[0], points[min]);
117+
std::swap(points[0], points[min]);
120118

121119
// Sort n-1 points with respect to the first point.
122120
Point p0;
@@ -143,7 +141,7 @@ stack<Point> convexHull(Point points[], const int n)
143141
m++; // Update size of modified array
144142
}
145143

146-
stack<Point> S;
144+
std::stack<Point> S;
147145
if (m < 3) return S;
148146

149147
S.push(points[0]);
@@ -165,9 +163,9 @@ stack<Point> convexHull(Point points[], const int n)
165163
}
166164

167165
// dim refers to the dimension that needs to be ignored
168-
vector<int> getConvexHull(const vector<vec3> &inPoints, const int dim)
166+
std::vector<int> getConvexHull(const std::vector<vec3> &inPoints, const int dim)
169167
{
170-
Point *myPoints = new Point[inPoints.size()];
168+
std::vector<Point> myPoints(inPoints.size());
171169
for(int i = 0; i < inPoints.size(); i++)
172170
{
173171
int index = 0;
@@ -182,19 +180,17 @@ vector<int> getConvexHull(const vector<vec3> &inPoints, const int dim)
182180
}
183181

184182
// TODO: myPoints gets overwritten...
185-
stack<Point> myStack = convexHull(myPoints, inPoints.size());
183+
auto myStack = convexHull(myPoints);
186184

187-
vector<int> ret;
185+
std::vector<int> ret;
188186
while (! myStack.empty() )
189187
{
190188
Point p = myStack.top();
191189
ret.push_back(p.index);
192190
myStack.pop();
193191
}
194-
delete [] myPoints;
195192

196193
return ret;
197194
}
198195

199196
#endif
200-

RBF.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ void RBF::setAcceleration(Acceleration myAcceleration)
6868
switch(this->acceleration_)
6969
{
7070
case FastMultipole:
71-
// TODO: memory leak?
72-
fmm_ = new FMM();
71+
fmm_.reset(new FMM);
7372
break;
7473
default:
7574
break;
@@ -96,9 +95,8 @@ void RBF::computeFunction()
9695
break;
9796

9897
case Random:
99-
bool *added;
10098
const int N = this->completeData_->fnc_.size();
101-
added = new bool[N];
99+
std::vector<bool> added(N);
102100
printf("%d\n", N);
103101

104102
for (int i = 0; i < N; i++)
@@ -152,7 +150,6 @@ void RBF::computeFunction()
152150
}
153151
}
154152
//printf("Total no. of data_ point: %d\n", this->data_->fnc_.size()); fflush(stdout);
155-
delete [] added;
156153
break;
157154
}
158155
}

RBF.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RBF
5959
Acceleration acceleration_;
6060
DataReduction dataReduction_;
6161

62-
FMM *fmm_;
62+
std::unique_ptr<FMM> fmm_;
6363

6464
void computeFunctionForData(); // throws std::runtime_error
6565
void computeErrorForData(std::vector<std::pair<double, int> > &error);

0 commit comments

Comments
 (0)