Permalink
Browse files

a really nasty memleak bug in __ccv_cache_nuke

still investigating why it is not revealed in earlier test
  • Loading branch information...
1 parent dfa13cb commit 28197f6f6ff9dc7590eeb6bb1a5053363ddd2c05 @liuliu committed Sep 25, 2010
View
@@ -5,6 +5,6 @@ Redistribution and use in source and binary forms, with or without modification,
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-* Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+* Neither the name of the authors nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -18,8 +18,8 @@ int main(int argc, char** argv)
unsigned int elapsed_time = get_current_time();
ccv_sift_param_t param;
param.noctaves = 3;
- param.nlevels = 6;
- param.upsample = 0;
+ param.nlevels = 8;
+ param.up2x = 1;
param.edge_threshold = 10;
param.norm_threshold = 0;
param.peak_threshold = 0;
@@ -33,6 +33,7 @@ int main(int argc, char** argv)
int i, j, k;
ccv_dense_matrix_t* imx = ccv_dense_matrix_new(image->rows, image->cols, CCV_8U | CCV_C1, 0, 0);
memset(imx->data.ptr, 0, imx->rows * imx->step);
+ int match = 0;
for (i = 0; i < obj_keypoints->rnum; i++)
{
float* odesc = obj_desc->data.fl + i * 128;
@@ -63,10 +64,13 @@ int main(int argc, char** argv)
int ix = (int)(kp->x + 0.5);
int iy = (int)(kp->y + 0.5);
imx->data.ptr[ix + iy * imx->step] = 255;
+ match++;
}
}
+ printf("%d keypoints out of %d are matched\n", match, obj_keypoints->rnum);
int len;
ccv_serialize(imx, "match.png", &len, CCV_SERIAL_PNG_FILE, 0);
+ ccv_matrix_free(imx);
ccv_array_free(obj_keypoints);
ccv_array_free(image_keypoints);
ccv_matrix_free(obj_desc);
View
@@ -167,8 +167,8 @@ ccv_dense_matrix_t* ccv_dense_matrix_new(int rows, int cols, int type, void* dat
ccv_dense_matrix_t ccv_dense_matrix(int rows, int cols, int type, void* data, uint64_t sig);
ccv_sparse_matrix_t* ccv_sparse_matrix_new(int rows, int cols, int type, int major, uint64_t sig);
uint64_t ccv_matrix_generate_signature(const char* msg, int len, uint64_t sig_start, ...);
-void ccv_matrix_free(ccv_matrix_t* mat);
void ccv_matrix_free_immediately(ccv_matrix_t* mat);
+void ccv_matrix_free(ccv_matrix_t* mat);
void ccv_garbage_collect();
void ccv_disable_cache();
void ccv_enable_cache();
@@ -529,7 +529,7 @@ typedef struct {
} ccv_keypoint_t;
typedef struct {
- int upsample;
+ int up2x;
int noctaves;
int nlevels;
float edge_threshold;
View
@@ -23,7 +23,7 @@ double ccv_normalize(ccv_matrix_t* a, ccv_matrix_t** b, int btype, int l_type)
uint64_t sig = (da->sig == 0) ? 0 : ccv_matrix_generate_signature(identifier, 20, da->sig, 0);
btype = (btype == 0) ? CCV_GET_DATA_TYPE(da->type) | CCV_C1 : CCV_GET_DATA_TYPE(btype) | CCV_C1;
ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, da->rows, da->cols, CCV_ALL_DATA_TYPE | CCV_C1, btype, sig);
- ccv_cache_return(db, );
+ ccv_cache_return(db, 0);
double sum = 0;
int i, j;
unsigned char* a_ptr = da->data.ptr;
View
@@ -84,6 +84,7 @@ ccv_matrix_t* ccv_cache_get(ccv_cache_t* cache, uint64_t sign)
int ccv_cache_put(ccv_cache_t* cache, uint64_t sign, ccv_matrix_t* x)
{
+ assert(((uint64_t)x & 0x3) == 0);
if (cache->rnum == 0)
{
cache->origin.terminal.off = (uint64_t)x | 0x1;
@@ -110,6 +111,7 @@ int ccv_cache_put(ccv_cache_t* cache, uint64_t sign, ccv_matrix_t* x)
uint64_t j = 63;
j = j << (depth * 6);
int dice, udice;
+ assert(depth < 10);
for (i = depth; i < 10; i++)
{
dice = (t.terminal.sign & j) >> (i * 6);
@@ -183,10 +185,7 @@ static void __ccv_cache_nuke(ccv_cache_index_t* branch)
uint64_t total = compute_bits(branch->branch.bitmap);
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
for (i = 0; i < total; i++)
- {
- if (!(set[i].terminal.off & 0x1))
- __ccv_cache_nuke(set + i);
- }
+ __ccv_cache_nuke(set + i);
free(set);
} else {
free((void*)(branch->terminal.off - (branch->terminal.off & 0x3)));
View
@@ -37,20 +37,29 @@ void ccv_unserialize(const char* in, ccv_dense_matrix_t** x, int type)
case CCV_SERIAL_JPEG_FILE:
__ccv_unserialize_jpeg_fd(fd, x, ctype);
if (*x != 0)
+ {
(*x)->sig = ccv_matrix_generate_signature((char*) (*x)->data.ptr, (*x)->rows * (*x)->step, 0);
+ (*x)->type &= ~CCV_REUSABLE;
+ }
break;
#endif
#ifdef HAVE_LIBPNG
case CCV_SERIAL_PNG_FILE:
__ccv_unserialize_png_fd(fd, x, ctype);
if (*x != 0)
+ {
(*x)->sig = ccv_matrix_generate_signature((char*) (*x)->data.ptr, (*x)->rows * (*x)->step, 0);
+ (*x)->type &= ~CCV_REUSABLE;
+ }
break;
#endif
case CCV_SERIAL_BMP_FILE:
__ccv_unserialize_bmp_fd(fd, x, ctype);
if (*x != 0)
+ {
(*x)->sig = ccv_matrix_generate_signature((char*) (*x)->data.ptr, (*x)->rows * (*x)->step, 0);
+ (*x)->type &= ~CCV_REUSABLE;
+ }
break;
}
View
@@ -87,6 +87,40 @@ ccv_sparse_matrix_t* ccv_sparse_matrix_new(int rows, int cols, int type, int maj
return mat;
}
+void ccv_matrix_free_immediately(ccv_matrix_t* mat)
+{
+ int type = *(int*)mat;
+ if (type & CCV_MATRIX_DENSE)
+ {
+ ccv_dense_matrix_t* dmt = (ccv_dense_matrix_t*)mat;
+ dmt->refcount = 0;
+ free(dmt);
+ } else if (type & CCV_MATRIX_SPARSE) {
+ ccv_sparse_matrix_t* smt = (ccv_sparse_matrix_t*)mat;
+ int i;
+ for (i = 0; i < CCV_GET_SPARSE_PRIME(smt->prime); i++)
+ if (smt->vector[i].index != -1)
+ {
+ ccv_dense_vector_t* iter = &smt->vector[i];
+ free(iter->data.ptr);
+ iter = iter->next;
+ while (iter != 0)
+ {
+ ccv_dense_vector_t* iter_next = iter->next;
+ free(iter->data.ptr);
+ free(iter);
+ iter = iter_next;
+ }
+ }
+ free(smt->vector);
+ free(smt);
+ } else if ((type & CCV_MATRIX_CSR) || (type & CCV_MATRIX_CSC)) {
+ ccv_compressed_sparse_matrix_t* csm = (ccv_compressed_sparse_matrix_t*)mat;
+ csm->refcount = 0;
+ free(csm);
+ }
+}
+
void ccv_matrix_free(ccv_matrix_t* mat)
{
int type = *(int*)mat;
View
@@ -142,15 +142,15 @@ static void __ccv_precomputed_expn()
void ccv_sift(ccv_dense_matrix_t* a, ccv_array_t** _keypoints, ccv_dense_matrix_t** _desc, int type, ccv_sift_param_t params)
{
assert(CCV_GET_CHANNEL(a->type) == CCV_C1);
- ccv_dense_matrix_t** g = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels + 1) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- memset(g, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels + 1) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- ccv_dense_matrix_t** dog = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- memset(dog, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- ccv_dense_matrix_t** th = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- memset(th, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- ccv_dense_matrix_t** md = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- memset(md, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.upsample ? params.noctaves + 1 : params.noctaves));
- if (params.upsample)
+ ccv_dense_matrix_t** g = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels + 1) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ memset(g, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels + 1) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ ccv_dense_matrix_t** dog = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ memset(dog, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ ccv_dense_matrix_t** th = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ memset(th, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ ccv_dense_matrix_t** md = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ memset(md, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 3) * (params.up2x ? params.noctaves + 1 : params.noctaves));
+ if (params.up2x)
{
g += params.nlevels + 1;
dog += params.nlevels - 1;
@@ -167,10 +167,12 @@ void ccv_sift(ccv_dense_matrix_t* a, ccv_array_t** _keypoints, ccv_dense_matrix_
double sigma0 = 1.6;
double sigmak = pow(2.0, 1.0 / (params.nlevels - 3));
double dsigma0 = sigma0 * sigmak * sqrt(1.0 - 1.0 / (sigmak * sigmak));
- if (params.upsample)
+ if (params.up2x)
{
ccv_sample_up(a, &g[-(params.nlevels + 1)], 0);
- double sd = sqrt(sigma0 * sigma0 - 1.0);
+ /* since there is a gaussian filter in sample_up function already,
+ * the default sigma for upsampled image is sqrt(2) */
+ double sd = sqrt(sigma0 * sigma0 - 2.0);
ccv_blur(g[-(params.nlevels + 1)], &g[-(params.nlevels + 1) + 1], CCV_32F | CCV_C1, sd);
ccv_matrix_free(g[-(params.nlevels + 1)]);
for (j = 1; j < params.nlevels; j++)
@@ -182,6 +184,7 @@ void ccv_sift(ccv_dense_matrix_t* a, ccv_array_t** _keypoints, ccv_dense_matrix_
ccv_gradient(g[-(params.nlevels + 1) + j], &th[-(params.nlevels - 3) + j - 2], 0, &md[-(params.nlevels - 3) + j - 2], 0, 1, 1);
ccv_matrix_free(g[-(params.nlevels + 1) + j]);
}
+ ccv_matrix_free(g[-1]);
}
double sd = sqrt(sigma0 * sigma0 - 0.25);
g[0] = a;
@@ -219,7 +222,7 @@ void ccv_sift(ccv_dense_matrix_t* a, ccv_array_t** _keypoints, ccv_dense_matrix_
if (!custom_keypoints)
{
/* detect keypoint */
- for (i = (params.upsample ? -1 : 0); i < params.noctaves; i++)
+ for (i = (params.up2x ? -1 : 0); i < params.noctaves; i++)
{
double s = pow(2.0, i);
int rows = dog[i * (params.nlevels - 1)]->rows;
@@ -446,9 +449,9 @@ void ccv_sift(ccv_dense_matrix_t* a, ccv_array_t** _keypoints, ccv_dense_matrix_
fdesc += 128;
}
}
- for (i = (params.upsample ? -(params.nlevels - 1) : 0); i < (params.nlevels - 1) * params.noctaves; i++)
+ for (i = (params.up2x ? -(params.nlevels - 1) : 0); i < (params.nlevels - 1) * params.noctaves; i++)
ccv_matrix_free(dog[i]);
- for (i = (params.upsample ? -(params.nlevels - 3) : 0); i < (params.nlevels - 3) * params.noctaves; i++)
+ for (i = (params.up2x ? -(params.nlevels - 3) : 0); i < (params.nlevels - 3) * params.noctaves; i++)
{
ccv_matrix_free(th[i]);
ccv_matrix_free(md[i]);
View
@@ -17,7 +17,7 @@ int main(int argc, char** argv)
b->data.db[4] = 1031;
b->data.db[5] = 1032;
ccv_dense_matrix_t* y = 0;
- ccv_gemm(a, b, 1, 0, 0, CCV_A_TRANSPOSE, &y, 0);
+ ccv_gemm(a, b, 1, 0, 0, CCV_A_TRANSPOSE, (ccv_matrix_t**)&y, 0);
printf("%f %f\n%f %f\n", y->data.db[0], y->data.db[1], y->data.db[2], y->data.db[3]);
ccv_matrix_free(a);
ccv_matrix_free(b);
View
@@ -77,7 +77,6 @@ static int FastAtan2_32f(const float *Y, const float *X, float *angle, int len )
// a4 <- (x < y ? a4 - z4 : a4 + z4)
a4 = _mm_mul_ps(_mm_add_ps(_mm_xor_ps(z4, _mm_andnot_ps(absmask, xly)), a4), scale4);
- __m128 m4 = _mm_sqrt_ps(_mm_add_ps(xq4, yq4));
_mm_storeu_ps(angle + i, a4);
}
#endif
@@ -2,7 +2,7 @@
int main(int argc, char** argv)
{
- ccv_sparse_matrix_t* mat = ccv_sparse_matrix_new(3, 3, CCV_32F | CCV_C1, CCV_SPARSE_ROW_MAJOR, NULL);
+ ccv_sparse_matrix_t* mat = ccv_sparse_matrix_new(3, 3, CCV_32F | CCV_C1, CCV_SPARSE_ROW_MAJOR, 0);
float cell;
cell = 1.0;
ccv_set_sparse_matrix_cell(mat, 0, 0, &cell);
@@ -16,7 +16,7 @@ int main(int argc, char** argv)
ccv_set_sparse_matrix_cell(mat, 2, 1, &cell);
cell = 6.0;
ccv_set_sparse_matrix_cell(mat, 2, 2, &cell);
- ccv_compressed_sparse_matrix_t* csm = NULL;
+ ccv_compressed_sparse_matrix_t* csm = 0;
ccv_compress_sparse_matrix(mat, &csm);
int i, j;
for (i = 0; i < csm->nnz; i++)
@@ -28,14 +28,14 @@ int main(int argc, char** argv)
for (i = 0; i < csm->rows + 1; i++)
printf("%d ", csm->offset[i]);
printf("\n");
- ccv_sparse_matrix_t* smt = NULL;
+ ccv_sparse_matrix_t* smt = 0;
ccv_decompress_sparse_matrix(csm, &smt);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
ccv_matrix_cell_t cell = ccv_get_sparse_matrix_cell(smt, i, j);
- printf("%f ", (cell.ptr != NULL) ? cell.fl[0] : 0);
+ printf("%f ", (cell.ptr != 0) ? cell.fl[0] : 0);
}
printf("\n");
}
View
@@ -18,7 +18,7 @@ int main(int argc, char** argv)
for (j = 0; j < b->cols; j++)
b->data.fl[i * b->cols + j] /= tb;
ccv_dense_matrix_t* x = 0;
- ccv_filter(a, b, &x, 0);
+ ccv_filter(a, b, (ccv_matrix_t**)&x, 0);
ccv_dense_matrix_t* imx = ccv_dense_matrix_new(x->rows, x->cols, CCV_8U | CCV_C1, 0, 0);
for (i = 0; i < x->rows; i++)
View
@@ -7,7 +7,7 @@ int main(int argc, char** argv)
ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 10, CCV_32F | CCV_C1, 0, 0);
for (i = 0; i < 10; i++)
dmt->data.fl[i] = i;
- ccv_normalize(dmt, &dmt, 0, CCV_L2_NORM);
+ ccv_normalize(dmt, (ccv_matrix_t**)&dmt, 0, CCV_L2_NORM);
for (i = 0; i < 10; i++)
printf("%f ", dmt->data.fl[i]);
printf("\n");
View
@@ -25,7 +25,7 @@ int main(int argc, char** argv)
ccv_sift_param_t param;
param.noctaves = 3;
param.nlevels = 6;
- param.upsample = 1;
+ param.up2x = 1;
param.edge_threshold = 10;
param.norm_threshold = 0;
param.peak_threshold = 0;
@@ -34,7 +34,7 @@ int main(int argc, char** argv)
printf("%d\n", keypoints->rnum);
ccv_dense_matrix_t* imx = ccv_dense_matrix_new(image->rows, image->cols, CCV_8U | CCV_C1, 0, 0);
memset(imx->data.ptr, 0, imx->rows * imx->step);
- int i, j;
+ int i;
for (i = 0; i < keypoints->rnum; i++)
{
ccv_keypoint_t* kp = (ccv_keypoint_t*)ccv_array_get(keypoints, i);
View
@@ -5,7 +5,7 @@ int main(int argc, char** argv)
ccv_dense_matrix_t* image = 0;
ccv_unserialize(argv[1], &image, CCV_SERIAL_ANY_FILE);
ccv_dense_matrix_t* b = 0;
- ccv_slice(image, &b, 0, 33, 41, 111, 91);
+ ccv_slice(image, (ccv_matrix_t**)&b, 0, 33, 41, 111, 91);
int len, quality = 95;
ccv_serialize(b, argv[2], &len, CCV_SERIAL_JPEG_FILE, &quality);
ccv_matrix_free(image);
@@ -3,7 +3,7 @@
int main(int argc, char** argv)
{
- ccv_sparse_matrix_t* mat = ccv_sparse_matrix_new(1000, 1000, CCV_32S | CCV_C1, CCV_SPARSE_ROW_MAJOR, NULL);
+ ccv_sparse_matrix_t* mat = ccv_sparse_matrix_new(1000, 1000, CCV_32S | CCV_C1, CCV_SPARSE_ROW_MAJOR, 0);
int i, j, k, n;
k = 0;
for (i = 0; i < 1000; i++)
@@ -20,7 +20,7 @@ int main(int argc, char** argv)
for (j = 0; j < 1000; j++)
{
ccv_matrix_cell_t cell = ccv_get_sparse_matrix_cell(mat, i, j);
- if (cell.ptr != NULL)
+ if (cell.ptr != 0)
assert(cell.i[0] == k);
else {
printf("ERROR: %d %d\n", i, j);

0 comments on commit 28197f6

Please sign in to comment.