Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
unref'ing SGReferenced data twice is OK now
- make ref() function protected: when assigning SGVector/Matrix objects
they will automagically call ref() so an extra ref() is not necessary
- ensure that when calling unref() the ptr to the refcount and the data
is set to NULL - this is OK since there must be one object that still
exists that will then later free the data. this enables calling unref()
	twice w/o getting double free errors
  • Loading branch information
Soeren Sonnenburg committed May 8, 2012
1 parent 67b73a2 commit 9490ebe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
14 changes: 2 additions & 12 deletions src/shogun/classifier/GaussianNaiveBayes.cpp
Expand Up @@ -96,23 +96,13 @@ bool CGaussianNaiveBayes::train(CFeatures* data)
m_dim = m_features->get_dim_feature_space();

// allocate memory for distributions' parameters and a priori probability
m_means.matrix = SG_MALLOC(float64_t, m_num_classes*m_dim);
m_means.num_rows = m_dim;
m_means.num_cols = m_num_classes;

m_variances.matrix = SG_MALLOC(float64_t, m_num_classes*m_dim);
m_variances.num_rows = m_dim;
m_variances.num_cols = m_num_classes;

m_means=SGMatrix<float64_t>(m_dim,m_num_classes);
m_variances=SGMatrix<float64_t>(m_dim, m_num_classes);
m_label_prob=SGVector<float64_t>(m_num_classes);

// allocate memory for label rates
m_rates=SGVector<float64_t>(m_num_classes);

// assure that memory is allocated
ASSERT(m_means.matrix);
ASSERT(m_variances.matrix);

// make arrays filled by zeros before using
m_means.zero();
m_variances.zero();
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/converter/LinearLocalTangentSpaceAlignment.cpp
Expand Up @@ -41,8 +41,8 @@ SGMatrix<float64_t> CLinearLocalTangentSpaceAlignment::construct_embedding(CFeat
int i,j;

SGMatrix<float64_t> feature_matrix = simple_features->get_feature_matrix().clone();
int N;
int dim;
int N=feature_matrix.num_cols;
int dim=feature_matrix.num_rows;
ASSERT(dimension<=dim);
float64_t* XTM = SG_MALLOC(float64_t, dim*N);
float64_t* lhs_M = SG_MALLOC(float64_t, dim*dim);
Expand Down
43 changes: 24 additions & 19 deletions src/shogun/lib/SGReferencedData.h
Expand Up @@ -56,24 +56,6 @@ class SGReferencedData
}

#ifdef USE_REFERENCE_COUNTING
/** increase reference counter
*
* @return reference count
*/
int32_t ref()
{
if (m_refcount == NULL)
{
return -1;
}

++(*m_refcount);
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", *m_refcount, this);
#endif
return *m_refcount;
}

/** display reference counter
*
* @return reference count
Expand All @@ -99,6 +81,7 @@ class SGReferencedData
if (m_refcount == NULL)
{
init_data();
m_refcount=NULL;
return -1;
}

Expand All @@ -118,7 +101,9 @@ class SGReferencedData
SG_SGCDEBUG("unref() refcount %d data %p decreased\n", *m_refcount, this);
#endif
init_data();
return *m_refcount;
int c=*m_refcount;
m_refcount=NULL;
return c;
}
}

Expand All @@ -130,6 +115,26 @@ class SGReferencedData
m_refcount=orig.m_refcount;
}

#ifdef USE_REFERENCE_COUNTING
/** increase reference counter
*
* @return reference count
*/
int32_t ref()
{
if (m_refcount == NULL)
{
return -1;
}

++(*m_refcount);
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", *m_refcount, this);
#endif
return *m_refcount;
}
#endif //USE_REFERENCE_COUNTING

/** needs to be overridden to copy data */
virtual void copy_data(const SGReferencedData &orig)=0;

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/ui/SGInterface.cpp
Expand Up @@ -3244,7 +3244,7 @@ CKernel* CSGInterface::create_kernel()
else if (strmatch(type, "CUSTOM"))
{
if (m_nrhs!=4 || !create_return_values(0))
return false;
return NULL;

float64_t* kmatrix=NULL;
int32_t num_feat=0;
Expand Down

0 comments on commit 9490ebe

Please sign in to comment.