Memory leak problem?

  • Thread starter Philip Ogunbona
  • Start date
P

Philip Ogunbona

This code is an implementation of class member function. The whole code
where the class and the member function is being used compiles and works
as expected. The problem is that I am returning a pointer to a container
I created in this member function and I have not DELETED it. Does
this not constitute a memory leak? How do I get around it without
changing the signature of the member function?

Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

vector<int> *classVector = new vector<int>(testvector.size());

vector<pair<int,double> > distance(numFeatures); // numFeatures is a
data member of class kNN

for (int i =0; i < testvector.size(); i++)
{
for (int j = 0; j < numFeatures; j++)
{
// pick up the class of the feature; it is the last element of the vector
distance[j].first = int(trainData[j][vecLen-1]);
//compute L2 distance between probe/testvector and each of the
training vector
distance[j].second = euclidean(testvector, trainData[j]);
}
//sort the distance vector in ascending order of magnitude
stable_sort(distance.begin(), distance.end(), lessDistance());
//pick the class with the most appearance in the top k entries
(*classVector)=k_vote(distance);

}
#ifdef DEBUG
copy((*classVector).begin(), (*classVector).end(),
std::eek:stream_iterator<int>(cout, " "));
cout << endl;
#endif
return classVector;

}
 
B

Bo Persson

Philip said:
This code is an implementation of class member function. The whole
code where the class and the member function is being used compiles
and works as expected. The problem is that I am returning a pointer
to a container I created in this member function and I have not
DELETED it. Does this not constitute a memory leak? How do I get
around it without
changing the signature of the member function?

Yes, a new without a delete is an obvious leak. :)

Someone has to do the delete at the proper moment, obviously. You can
either pass the responsibility on to the code calling the function, or
keep a copy of the pointer inside the class, so you can call the
delete later.

If it is at all possble to change the signature of the function, I
would return the vector buy value. That avoids your problem
altogether, instead of "solving" it.


Bo Persson
Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

vector<int> *classVector = new vector<int>(testvector.size());

vector<pair<int,double> > distance(numFeatures); // numFeatures is a
data member of class kNN

for (int i =0; i < testvector.size(); i++)
{
for (int j = 0; j < numFeatures; j++)
{
// pick up the class of the feature; it is the last element of the
vector distance[j].first = int(trainData[j][vecLen-1]);
//compute L2 distance between probe/testvector and each of the
training vector
distance[j].second = euclidean(testvector, trainData[j]);
}
//sort the distance vector in ascending order of magnitude
stable_sort(distance.begin(), distance.end(), lessDistance());
//pick the class with the most appearance in the top k entries
(*classVector)=k_vote(distance);

}
#ifdef DEBUG
copy((*classVector).begin(), (*classVector).end(),
std::eek:stream_iterator<int>(cout, " "));
cout << endl;
#endif
return classVector;

}
 
J

jkherciueh

Philip said:
This code is an implementation of class member function. The whole code
where the class and the member function is being used compiles and works
as expected. The problem is that I am returning a pointer to a container
I created in this member function and I have not DELETED it. Does
this not constitute a memory leak?

Not by and in itself. If you have a function that allocates memory and
returns a pointer to that region, the caller of the function has to take
responsibility for deallocating the memory. This needs to become part of
the contract and must be documented as such. (You also need to document how
the client needs to dispose off the pointee, i.e., whether to use delete,
delete[], free(), or some other method.)
How do I get around it without
changing the signature of the member function?

You don't. If you see shifting the responsibility of deleting the pointer to
the client as a problem, change the signature. Returning the container by
value isn't a bad thing. You could also consider the use of some
smart-pointer with appropriate semantics (but that would involve changing
the signature, too).
Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

vector<int> *classVector = new vector<int>(testvector.size());

vector<pair<int,double> > distance(numFeatures); // numFeatures is a
data member of class kNN

for (int i =0; i < testvector.size(); i++)
{
for (int j = 0; j < numFeatures; j++)
{
// pick up the class of the feature; it is the last element of the vector
distance[j].first = int(trainData[j][vecLen-1]);
//compute L2 distance between probe/testvector and each of the
training vector
distance[j].second = euclidean(testvector, trainData[j]);
}
//sort the distance vector in ascending order of magnitude
stable_sort(distance.begin(), distance.end(), lessDistance());
//pick the class with the most appearance in the top k entries
(*classVector)=k_vote(distance);

}
#ifdef DEBUG
copy((*classVector).begin(), (*classVector).end(),
std::eek:stream_iterator<int>(cout, " "));
cout << endl;
#endif
return classVector;

}



Best

Kai-Uwe Bux
 
S

Stingray

Thanks Bo. You are right, I just have to change the signature of the
function. It is a neater and safer solution.

Regards,

Philip O.

Bo said:
Philip said:
This code is an implementation of class member function. The whole
code where the class and the member function is being used compiles
and works as expected. The problem is that I am returning a pointer
to a container I created in this member function and I have not
DELETED it. Does this not constitute a memory leak? How do I get
around it without
changing the signature of the member function?

Yes, a new without a delete is an obvious leak. :)

Someone has to do the delete at the proper moment, obviously. You can
either pass the responsibility on to the code calling the function, or
keep a copy of the pointer inside the class, so you can call the
delete later.

If it is at all possble to change the signature of the function, I
would return the vector buy value. That avoids your problem
altogether, instead of "solving" it.


Bo Persson
Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

vector<int> *classVector = new vector<int>(testvector.size());

vector<pair<int,double> > distance(numFeatures); // numFeatures is a
data member of class kNN

for (int i =0; i < testvector.size(); i++)
{
for (int j = 0; j < numFeatures; j++)
{
// pick up the class of the feature; it is the last element of the
vector distance[j].first = int(trainData[j][vecLen-1]);
//compute L2 distance between probe/testvector and each of the
training vector
distance[j].second = euclidean(testvector, trainData[j]);
}
//sort the distance vector in ascending order of magnitude
stable_sort(distance.begin(), distance.end(), lessDistance());
//pick the class with the most appearance in the top k entries
(*classVector)=k_vote(distance);

}
#ifdef DEBUG
copy((*classVector).begin(), (*classVector).end(),
std::eek:stream_iterator<int>(cout, " "));
cout << endl;
#endif
return classVector;

}

 
S

Stingray

Thanks Kai-Uwe Bux, changing the signature is possible. It just looks
nice to have the signature in the current form. I'll re-code the
function and move on.

Regards,

Philip O.


Philip said:
This code is an implementation of class member function. The whole code
where the class and the member function is being used compiles and works
as expected. The problem is that I am returning a pointer to a container
I created in this member function and I have not DELETED it. Does
this not constitute a memory leak?

Not by and in itself. If you have a function that allocates memory and
returns a pointer to that region, the caller of the function has to take
responsibility for deallocating the memory. This needs to become part of
the contract and must be documented as such. (You also need to document how
the client needs to dispose off the pointee, i.e., whether to use delete,
delete[], free(), or some other method.)
How do I get around it without
changing the signature of the member function?

You don't. If you see shifting the responsibility of deleting the pointer to
the client as a problem, change the signature. Returning the container by
value isn't a bad thing. You could also consider the use of some
smart-pointer with appropriate semantics (but that would involve changing
the signature, too).
Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

vector<int> *classVector = new vector<int>(testvector.size());

vector<pair<int,double> > distance(numFeatures); // numFeatures is a
data member of class kNN

for (int i =0; i < testvector.size(); i++)
{
for (int j = 0; j < numFeatures; j++)
{
// pick up the class of the feature; it is the last element of the vector
distance[j].first = int(trainData[j][vecLen-1]);
//compute L2 distance between probe/testvector and each of the
training vector
distance[j].second = euclidean(testvector, trainData[j]);
}
//sort the distance vector in ascending order of magnitude
stable_sort(distance.begin(), distance.end(), lessDistance());
//pick the class with the most appearance in the top k entries
(*classVector)=k_vote(distance);

}
#ifdef DEBUG
copy((*classVector).begin(), (*classVector).end(),
std::eek:stream_iterator<int>(cout, " "));
cout << endl;
#endif
return classVector;

}



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top