Skip to content

Commit

Permalink
fix a couple of memory leaks in so example (mostly unnecessary SG_REF's
Browse files Browse the repository at this point in the history
		of newly created objects that are returned)
  • Loading branch information
Soeren Sonnenburg committed Aug 31, 2012
1 parent 958ce8f commit 9e92392
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
26 changes: 11 additions & 15 deletions examples/undocumented/libshogun/so_multiclass_BMRM.cpp
Expand Up @@ -41,7 +41,7 @@ char FNAME[] = "data.svmlight";
* @param labs vector with labels
* @param feats matrix with features
*/
void read_data(const char fname[], uint32_t DIM, uint32_t N, SGVector< float64_t > *labs, SGMatrix< float64_t > *feats)
void read_data(const char fname[], uint32_t DIM, uint32_t N, SGVector<float64_t> labs, SGMatrix<float64_t> feats)
{
CStreamingAsciiFile* file=new CStreamingAsciiFile(fname);
SG_REF(file);
Expand All @@ -56,17 +56,15 @@ void read_data(const char fname[], uint32_t DIM, uint32_t N, SGVector< float64_t

uint32_t num_vectors=0;

while( stream_features->get_next_example() )
while (stream_features->get_next_example())
{
vec.zero();
stream_features->add_to_dense_vec(1.0, vec, DIM);

(*labs)[num_vectors]=stream_features->get_label();
labs[num_vectors]=stream_features->get_label();

for(uint32_t i=0; i<DIM; ++i)
{
(*feats)[num_vectors*DIM+i]=vec[i];
}
for (uint32_t i=0; i<DIM; ++i)
feats[num_vectors*DIM+i]=vec[i];

num_vectors++;
stream_features->release_example();
Expand Down Expand Up @@ -175,15 +173,13 @@ int main(int argc, char * argv[])
solver=BMRM;
}

SGVector< float64_t >* labs=
new SGVector< float64_t >(num_feat);
SGVector<float64_t> labs(num_feat);

SGMatrix< float64_t >* feats=
new SGMatrix< float64_t >(feat_dim, num_feat);
SGMatrix<float64_t> feats(feat_dim, num_feat);

if (argc==1)
{
gen_rand_data(*labs, *feats);
gen_rand_data(labs, feats);
}
else
{
Expand All @@ -192,11 +188,11 @@ int main(int argc, char * argv[])
}

// Create train labels
CMulticlassSOLabels* labels = new CMulticlassSOLabels(*labs);
CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs);

// Create train features
CDenseFeatures< float64_t >* features =
new CDenseFeatures< float64_t >(*feats);
new CDenseFeatures< float64_t >(feats);

// Create structured model
CMulticlassModel* model = new CMulticlassModel(features, labels);
Expand Down Expand Up @@ -244,7 +240,7 @@ int main(int argc, char * argv[])

for (uint32_t i=0; i<num_feat; ++i)
{
error+=(( (CRealNumber*) out->get_label(i) )->value==labs->get_element(i)) ? 0.0 : 1.0;
error+=(( (CRealNumber*) out->get_label(i) )->value==labs.get_element(i)) ? 0.0 : 1.0;
}

SG_SPRINT("Error = %lf %% \n", error/num_feat*100);
Expand Down
1 change: 0 additions & 1 deletion src/shogun/machine/LinearStructuredOutputMachine.cpp
Expand Up @@ -72,7 +72,6 @@ CStructuredLabels* CLinearStructuredOutputMachine::apply_structured(CFeatures* d
}
}
SG_UNREF(model_features);
SG_REF(out);
return out;

This comment has been minimized.

Copy link
@iglesias

iglesias Sep 2, 2012

Collaborator

Why is it that we do not have to do SG_REF here? Since the object out is created
in this method and it is the return value I was confident that it should be done in
that way.

However, I have checked the others apply_* and see that SG_REF is not done
there either. What have I understood wrong?

This comment has been minimized.

Copy link
@sonney2k

sonney2k via email Sep 2, 2012

Member

This comment has been minimized.

Copy link
@iglesias

iglesias Sep 2, 2012

Collaborator

Internally like for example?

With internally I understand in other part of shogun. The method
apply for example is used in CrossValidation and I understand
this as internally too.

This comment has been minimized.

Copy link
@sonney2k

sonney2k via email Sep 2, 2012

Member
}

Expand Down
1 change: 0 additions & 1 deletion src/shogun/structure/MulticlassModel.cpp
Expand Up @@ -100,7 +100,6 @@ CResultSet* CMulticlassModel::argmax(
// Build the CResultSet object to return
CResultSet* ret = new CResultSet();
CRealNumber* y = new CRealNumber(ypred);
SG_REF(ret);
SG_REF(y);

ret->psi_pred = get_joint_feature_vector(feat_idx, y);
Expand Down

0 comments on commit 9e92392

Please sign in to comment.