Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
overload assignment operator and add GC debug output for sgvector
  • Loading branch information
Soeren Sonnenburg committed May 3, 2012
1 parent 2d8d9bc commit 786790d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/shogun/features/Labels.cpp
Expand Up @@ -72,12 +72,18 @@ void CLabels::init()
SG_REF(m_subset_stack);
}

void CLabels::set_labels(const SGVector<float64_t> v)
void CLabels::set_labels(SGVector<float64_t> v)
{
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot set labels\n");

SG_PRINT("before labels=v\n");
labels.ref_count();
v.ref_count();
labels=v;
labels.ref_count();
v.ref_count();
SG_PRINT("after labels=v\n");
}

bool CLabels::is_two_class_labeling()
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/Labels.h
Expand Up @@ -165,7 +165,7 @@ class CLabels : public CSGObject
*
* @param v labels
*/
void set_labels(const SGVector<float64_t> v);
void set_labels(SGVector<float64_t> v);

/**
* set all labels to +1
Expand Down
16 changes: 16 additions & 0 deletions src/shogun/lib/SGVector.h
Expand Up @@ -12,6 +12,8 @@
#ifndef __SGVECTOR_H__
#define __SGVECTOR_H__

#include <shogun/io/SGIO.h>

namespace shogun
{
/** @brief shogun vector */
Expand Down Expand Up @@ -50,6 +52,16 @@ template<class T> class SGVector
ref();
}

/** override assignment operator to increase refcount on assignments */
SGVector& operator= (const SGVector &orig)
{
vector=orig.vector;
vlen=orig.vlen;
m_refcount=orig.m_refcount;
ref();
return *this;
}

/** empty destructor */
virtual ~SGVector()
{
Expand All @@ -69,6 +81,7 @@ template<class T> class SGVector
}

++(*m_refcount);
SG_SGCDEBUG("ref() refcount %ld vec %p (%p) increased\n", *m_refcount, vector, this);
return *m_refcount;
}

Expand All @@ -81,6 +94,7 @@ template<class T> class SGVector
if (m_refcount == NULL)
return -1;

SG_SGCDEBUG("ref_count(): refcount %d, vec %p (%p)\n", *m_refcount, vector, this);
return *m_refcount;
}

Expand All @@ -96,6 +110,7 @@ template<class T> class SGVector

if (*m_refcount==0 || --(*m_refcount)==0)
{
SG_SGCDEBUG("unref() refcount %d vec %p (%p) destroying\n", *m_refcount, vector, this);
SG_FREE(vector);
SG_FREE(m_refcount);

Expand All @@ -107,6 +122,7 @@ template<class T> class SGVector
}
else
{
SG_SGCDEBUG("unref() refcount %d vec %p (%p) decreased\n", *m_refcount, vector, this);
return *m_refcount;
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/shogun/machine/KernelMachine.cpp
Expand Up @@ -71,9 +71,6 @@ CKernelMachine::~CKernelMachine()
SG_UNREF(kernel);
SG_UNREF(m_custom_kernel);
SG_UNREF(m_kernel_backup);

SG_FREE(m_alpha.vector);
SG_FREE(m_svs.vector);
}

void CKernelMachine::set_kernel(CKernel* k)
Expand Down

0 comments on commit 786790d

Please sign in to comment.