Skip to content

Commit 3f97acf

Browse files
authored
Moved cudaMalloc() and cudaFree() to the constructor and destructor to avoid memory reallocation
1 parent 53c8405 commit 3f97acf

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

modules/cudafeatures2d/src/fast.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace
6666
{
6767
public:
6868
FAST_Impl(int threshold, bool nonmaxSuppression, int max_npoints);
69+
~FAST_Impl();
6970

7071
virtual void detect(InputArray _image, std::vector<KeyPoint>& keypoints, InputArray _mask);
7172
virtual void detectAsync(InputArray _image, OutputArray _keypoints, InputArray _mask, Stream& stream);
@@ -95,6 +96,12 @@ namespace
9596
FAST_Impl::FAST_Impl(int threshold, bool nonmaxSuppression, int max_npoints) :
9697
threshold_(threshold), nonmaxSuppression_(nonmaxSuppression), max_npoints_(max_npoints)
9798
{
99+
cudaSafeCall( cudaMalloc(&d_counter, sizeof(unsigned int)) );
100+
}
101+
102+
FAST_Impl::~FAST_Impl()
103+
{
104+
cudaSafeCall( cudaFree(d_counter) );
98105
}
99106

100107
void FAST_Impl::detect(InputArray _image, std::vector<KeyPoint>& keypoints, InputArray _mask)
@@ -116,8 +123,6 @@ namespace
116123
{
117124
using namespace cv::cuda::device::fast;
118125

119-
cudaSafeCall( cudaMalloc(&d_counter, sizeof(unsigned int)) );
120-
121126
const GpuMat img = _image.getGpuMat();
122127
const GpuMat mask = _mask.getGpuMat();
123128

@@ -141,7 +146,6 @@ namespace
141146
if (count == 0)
142147
{
143148
_keypoints.release();
144-
cudaSafeCall( cudaFree(d_counter) );
145149
return;
146150
}
147151

@@ -166,8 +170,6 @@ namespace
166170
kpLoc.colRange(0, count).copyTo(locRow, stream);
167171
keypoints.row(1).setTo(Scalar::all(0), stream);
168172
}
169-
170-
cudaSafeCall( cudaFree(d_counter) );
171173
}
172174

173175
void FAST_Impl::convert(InputArray _gpu_keypoints, std::vector<KeyPoint>& keypoints)

0 commit comments

Comments
 (0)