473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problems with destructor

23
Hi all,

I'm having a problem with a program that continually generates assertion errors. After stepping through in the debugger and inserting several lines to track the progress, I've found it is attempting to call the class destructor 2 times more than it calls the class constructor. As a result, it's trying to delete[] memory that has never been allocated, and causing a problem.

I can make the problem go away by simply removing the delete[] statement from the destructor, but I know that's not right.

It's a textbook project on overloading operators, and I'm wondering if it's something to do with implicitly created temporary objects, but I would think the compiler would have to call the constructors when it created those temporary objects. It's not. I've basically copied the code directly out of the textbook, though changed the operations and types of course to suit the assignment, but the definitions and syntax is identical. All I changed was the meat of the functions, and that's not what's causing the problems.

I can post code if desired, but it's not short...
Sep 30 '06 #1
2 1728
m013690
23
Never mind, I've got it figured out now. 'Twas a problem with a copy constructor... Guess I'm still learning.

What was happening (or so I believe I've discovered) was that the default copy constructor was copying over dynamically allocated memory pointers, so when the destructor was called it was releasing memory which was still being used by another object. I had to actually define a copy constructor instead of allowing the compiler to do it, since there was dynamically allocated memory involved (new [ ] operator). Any experts want to confirm for me that I've learned the proper lesson here?
Sep 30 '06 #2
Banfa
9,065 Expert Mod 8TB
Yes that sounds about right, for any class where 1 or more of the members is a pointer that you allocate memory to using new or malloc then if you use the default copy constructor you will get memory errors on class destruction.

This is because the default copy constructor just copies the members from 1 class to another but if some of the members are pointers to allocated memory what you need to do is allocate memory for the second class (the one being copied to ) and copy the contents of the memory.
Sep 30 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going...
3
by: jaws | last post by:
My problems is as follows. Hope you guys can provide some insight. 1. I have a class which opens a file and simply spits out the text. Methods in place to open, print, close, and delete the...
2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
15
by: Mark Gillespie | last post by:
I have a class that launches a process (amongst other things). I keep track of the process handle, and just prior to the obejct being destroyed by the garbage collector, I want to kill that...
7
by: greg | last post by:
Hi We have w2k, iis5, .NET/c# I periodically receive this message and the system freezes ++++++++++++++++++++++++++++++++++++++++++++++++++ Timeout expired. The timeout period elapsed...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
2
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing...
3
by: Bruno.DiStefano | last post by:
Hi All, BACKGROUND INFO I need to use a "vector" structure to store a number of objects that becomes known only at run time. The constructor, at instantiation time of a new object, increments a...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
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...

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.