Dear cpp-ians,
I have a class:
class mImage
{
public:
//constructors / destructor
mImage(unsigned int nrLayers);
~mImage();
//member functions
...
private:
//data container
unsigned int NbLayers;
vector<Image *> vectorImage;
vector<Image *>::iterator itVectorImage;
//data clearing
void clear();
};
and I have problems with me deconstructor:
mImage::~mImage()
{
clear();
}
void mImage::clear(void)
{
if(NbLayers>0)
{
for(unsigned int l=0;l<NbLayers;++l)
{
delete vectorImage[l];vectorImage[l]=NULL;
}
vectorImage.clear();
}
NbLayers=0;
}
The problem is that apparently the command
"delete vectorImage[l];vectorImage[l]=NULL;"
does not clear the images stored in it, but only the vector itself.
vectorImage is a vector<Image *> and for the class Image I have a
deconstructor command that works perfectly.
Any advice how I should clear the vectorImage together with all the
images that are in it?
Kind regards and thank you in advance,
Stef 3 2349
steflhermitte wrote: Dear cpp-ians,
I have a class:
class mImage { public: //constructors / destructor mImage(unsigned int nrLayers); ~mImage(); //member functions ...
private: //data container unsigned int NbLayers; vector<Image *> vectorImage; vector<Image *>::iterator itVectorImage;
//data clearing void clear();
};
and I have problems with me deconstructor:
mImage::~mImage() { clear(); }
void mImage::clear(void) { if(NbLayers>0) { for(unsigned int l=0;l<NbLayers;++l) { delete vectorImage[l];vectorImage[l]=NULL; } vectorImage.clear(); } NbLayers=0; }
The problem is that apparently the command "delete vectorImage[l];vectorImage[l]=NULL;" does not clear the images stored in it, but only the vector itself.
What do you mean by "apparently"? What makes you believe it doesn't work?
vectorImage is a vector<Image *> and for the class Image I have a deconstructor command that works perfectly.
Any advice how I should clear the vectorImage together with all the images that are in it?
What you are doing looks fine, unless NbLayers has the wrong value. Btw, you
don't need that member anyway. vectorImage.size() will give you the number
of elements in the vector.
Looking at the size of my Page File History, the memory is not cleared
sufficiently.
I thought it was because of the assignment of vectorImage:
void mImage::setImages()
{
itInputFileName = inputFileName.begin();
for (itVectorImage = vectorImage.begin(); itVectorImage!=
vectorImage.end(); itVectorImage++ )
{
Image *tempImage = new Image(*(itInputFileName));
*itVectorImage = &(*tempImage);
itInputFileName++;
}
}
vectorImage is thus a vector of pointers to images. When I delete
vectorImage, I was afraid the images were not cleared simultaneously.
Thanks for your help!
Stef
steflhermitte wrote: Looking at the size of my Page File History, the memory is not cleared sufficiently.
Your destructor code in an earlier post looked ok. Just because the
memory isn't freed back to the OS doesn't mean it isn't freed for later
use by the program itself. If you're worried about memory leaks, there
is software out there to help you with that, e.g., purify.
I thought it was because of the assignment of vectorImage:
void mImage::setImages() { itInputFileName = inputFileName.begin();
for (itVectorImage = vectorImage.begin(); itVectorImage!= vectorImage.end(); itVectorImage++ ) { Image *tempImage = new Image(*(itInputFileName)); *itVectorImage = &(*tempImage);
You don't need a temporary here. You can do it in one line:
*itVectorImage = new Image(*itInputFileName);
itInputFileName++; } }
You're right. This function might be a problem if vectorImage already
contains valid pointers to images. You aren't deleting the old
pointers before overwriting them. In that case, the memory they used
to point to will be leaked.
vectorImage is thus a vector of pointers to images. When I delete vectorImage, I was afraid the images were not cleared simultaneously.
Assuming vectorImage already contains valid pointers, you need the
line...
delete *itVectorImage;
....in your setImage() function before assigning a new object.
Kristo This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ron |
last post by:
All,
I have a weird problem,
I have a Native DLL that CoCreates a COM object. If I call this DLL from a
native MFC app, everything goes just fine; If I call it from an managed
application...
|
by: Tomomichi Amano |
last post by:
Hello
How can I delete (clear) lines that were made useing Graphics.DrawLine() ?
Thanks in advance! Have a nice day!
|
by: Doug Wiley |
last post by:
Hello All
For reference
MyClass myClass = new MyClass()
myClass.MyMethod()
It appears that if you throw an application exception in MyMethod(), the myClass deconstructor will be bypassed....
|
by: Steve Booth |
last post by:
I have a web form with a button and a placeholder, the button adds a user
control to the placeholder (and removes any existing controls). The user
control contains a single button. I have done all...
|
by: Yogi21 |
last post by:
Hi
I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I...
|
by: Xiaoshen Li |
last post by:
Dear All,
I am a little confused.
//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
|
by: Jim Rodgers |
last post by:
I have a big asp file that has an error under certain conditions -- totally
repeatable. However, it only fails when I set response.buffer = True at the
top. WHen I set it False in order to debug...
|
by: matt |
last post by:
hello,
so from time to time i have to write code to send a file or a stream
back to the end user. in my mini library i have code to do so. however,
some of the Response object properties arent...
|
by: comp.lang.php |
last post by:
I am using session_start() on index.php and for some reason sometimes
it'll fail. No warnings, no errors, no notices, not even after
prepending error_reporting(E_ALL) and ini_set('display_errors',...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |