473,672 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deconstructor fails to clear everything

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(v oid)
{
if(NbLayers>0)
{
for(unsigned int l=0;l<NbLayers; ++l)
{
delete vectorImage[l];vectorImage[l]=NULL;
}
vectorImage.cle ar();
}
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

Jul 23 '05 #1
3 2366
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(v oid)
{
if(NbLayers>0)
{
for(unsigned int l=0;l<NbLayers; ++l)
{
delete vectorImage[l];vectorImage[l]=NULL;
}
vectorImage.cle ar();
}
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.siz e() will give you the number
of elements in the vector.
Jul 23 '05 #2
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::setImag es()
{
itInputFileName = inputFileName.b egin();

for (itVectorImage = vectorImage.beg in(); itVectorImage!=
vectorImage.end (); itVectorImage++ )
{
Image *tempImage = new Image(*(itInput FileName));
*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

Jul 23 '05 #3
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::setImag es()
{
itInputFileName = inputFileName.b egin();

for (itVectorImage = vectorImage.beg in(); itVectorImage!=
vectorImage.end (); itVectorImage++ )
{
Image *tempImage = new Image(*(itInput FileName));
*itVectorImage = &(*tempImage );
You don't need a temporary here. You can do it in one line:
*itVectorImage = new Image(*itInputF ileName);
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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1898
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 with the exact same parameters, the CoCreate fails file E_NOINTERFACE (x80004002). I've played around with the different threading models (CoInitialize) but that does not help..
2
20927
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!
2
327
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. Can anyone confirm this please? I have a database layer class that disposes connections in the deconstructor, and I suspect the connections are staying open when exceptions occur elsewhere in the class.. Thanks
6
3370
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
2
1501
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 come to the last element, the BinarySearch method fails.It gives me a return value which is negative although the element is very much there. I am debugging through the code and I find nothing wrong with it.Note that i am using the first overloaded...
11
2943
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();
12
7900
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 it, it works every time! I even set it to True, but did a .Flush just before the error, and the error won't happen. It only happens when response.buffer is True and no .response.flush is issued. The error is a string variable turns-up empty...
4
6750
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 exactly clear to me, and for some reason they arent spec'd out in my v1.1 SDK. can anyone provide a simple break down of the intended purpose & appropiateness of these:
6
1963
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', TRUE) before session_start() do I see anything, it just plain dies. I used LiveHTTPHeaders to verify that I am getting a 200 OK response, thus everything seems OK except that it's dying on session_start(). Sometimes.
0
8486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8406
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8609
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7449
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.