Hi,
I have a program with several classes which i was debugging and just found out strange thing: it creates the object in a constructor(class Employee), allocates all memory for other class objects, and then goes straight to the destructor and deallocates all memory just allocated. Only then it goes to the function using this object and gives errors because the memory has just been deallocated.
Why is it doing so????
This is the class implementation: -
void Employee :: create(TEdit *EnterEmployeeName,
-
TEdit *EnterEmployeeSurname,
-
TEdit *EnterEmployeeSocialNumber,
-
TEdit *EnterEmployeeSalary,
-
TEdit *EnterEmployeeHouseName,
-
TEdit *EnterEmployeeHouseNo,
-
TEdit *EnterEmployeeStreet,
-
TEdit *EnterEmployeeTown,
-
TEdit *EnterEmployeePostCode,
-
TEdit *EnterEmployeeCountry,
-
TEdit *EnterDOBdate,
-
TEdit *EnterDOBmonth,
-
TEdit *EnterDOByear,
-
TEdit *EnterEmploymentStartDate,
-
TEdit *EnterEmploymentStartMonth,
-
TEdit *EnterEmploymentStartYear,
-
TEdit *EnterBankCode,
-
TEdit *EnterAccountNo,
-
TEdit *EnterBalance,
-
TEdit *EnterBankName,
-
TEdit *EnterRate,
-
TEdit *EnterOverdraftLimit,
-
TEdit *EnterConditions)
-
{
-
//save data for the employee
-
FirstName = EnterEmployeeName->Text;
-
Surname = EnterEmployeeSurname->Text;
-
Salary = StrToInt(EnterEmployeeSalary->Text);
-
SocialNumber = StrToFloat(EnterEmployeeSocialNumber->Text);
-
-
HomeAddress -> create(EnterEmployeeHouseName->Text,
-
StrToInt(EnterEmployeeHouseNo->Text),
-
EnterEmployeeStreet->Text,
-
EnterEmployeeTown->Text,
-
EnterEmployeePostCode->Text,
-
EnterEmployeeCountry->Text);
-
DOB->setDate(StrToInt(EnterDOBdate->Text),
-
StrToInt(EnterDOBmonth->Text),
-
StrToInt(EnterDOByear->Text));
-
-
EmploymentStartDate->setDate(StrToInt(EnterEmploymentStartDate->Text), StrToInt(EnterEmploymentStartMonth->Text), StrToInt(EnterEmploymentStartYear->Text));
-
-
theAccounts[0]->create(EnterBankCode->Text,
-
StrToInt(EnterAccountNo->Text),
-
StrToFloat(EnterBalance->Text),
-
EnterBankName->Text,
-
StrToFloat(EnterRate->Text),
-
StrToFloat(EnterOverdraftLimit->Text),
-
EnterConditions->Text);
-
}
-
-
Employee :: Employee()
-
{
-
FirstName = "";
-
Surname = "";
-
SocialNumber = -1;
-
Salary = -1;
-
DOB = new clsDate();
-
EmploymentStartDate = new clsDate();
-
-
HomeAddress = new Address();
-
-
theAccounts = new Account*[3];
-
theAccounts[0] = new CurrentAccount();
-
}
-
-
-
Employee :: ~Employee() /////program goes here straight from the constructor
-
{
-
-
delete HomeAddress;
-
HomeAddress = NULL;
-
-
-
delete DOB;
-
delete EmploymentStartDate;
-
-
delete theAccounts[0];
-
delete theAccounts;
-
}
-
-
-
And this is the function in my Form where i call this Employee class function create(), which is preformed after the destructor... -
-
void __fastcall TForm1::CreateEmployeeButtonClick(TObject *Sender)
-
{
-
theEmployeeList.addEmployee(i, Size);
-
-
theEmployeeList.getEmployee(i)->create(EnterEmployeeName,
-
EnterEmployeeSurname,
-
EnterEmployeeSocialNumber,
-
EnterEmployeeSalary,
-
EnterHouseName,
-
EnterHouseNo,
-
EnterStreet,
-
EnterTown,
-
EnterPostCode,
-
EnterCountry,
-
EnterDOBdate,
-
EnterDOBmonth,
-
EnterDOByear,
-
EnterEmploymentStartDate,
-
EnterEmploymentStartMonth,
-
EnterEmploymentStartYear,
-
EnterBankCode,
-
EnterAccountNo,
-
EnterBalance,
-
EnterBankName,
-
EnterRate,
-
EnterOverdraftLimit,
-
EnterConditions);
-
-
Could someone please help...
9 1832
Could you post the code of the following function? (addEmployee) -
theEmployeeList.addEmployee(i, Size);
-
Could you post the code of the following function? (addEmployee) -
theEmployeeList.addEmployee(i, Size);
-
Sure: -
-
void EmployeeList :: addEmployee(int &i, int &SIZE)
-
{
-
i = SIZE;
-
myEmployeeList.push_back(Employee());
-
SIZE++;
-
ListSize = SIZE;
-
}
-
-
What you are describing is the behavior between a base class and a derived class where the derived class' constructor throws an exception. In this case, the base class desctructor will be called.
The STL library uses this design where the base class handles memory allocation, and if the derived class blows up, the memory is guaranteed to be deallocated.
Since you haven't posted the class definition, does this match your situation?
-
myEmployeeList.push_back(Employee());
-
Im assuming this is a list (although it could be a vector, doesnt matter).
This code creates an object in the stack, at that moment the constructor is called.
Stack objects die when the function they are created in finishes (strictly speaking they die when the scope whithin they were created in finishes), so you see the destructor being called as a result of that; i.e. when addEmployee finishes the destructor gets called.
The STL list will create a copy of your stack object.
I am assuming you dont have neither a copy constructor nor an overloaded = operator in your Employee class, so the copy STL is doing is a bad copy, since you have pointers inside your Employee class, and when you try using them the program crashes because you have already disposed those pointers in the destructor.
So to solve this problem, create a copy constructor and overload the operator= of your Employee class, just make sure to deal with pointers accordingly inside both the copy constructor and the overladed = operator.
OR
have your list use heap objects rather than stack objects and use this: -
myEmployeeList.push_back(new Employee());
-
In the latter case, make sure to free all the pointers inside the list when needed.
by the way, it is not necessary to keep a count of how many employees you have entered into the list, the function count() will give you the current list size; or is it size()? I cant remember :S
-
myEmployeeList.push_back(Employee());
-
Im assuming this is a list (although it could be a vector, doesnt matter).
This code creates an object in the stack, at that moment the constructor is called.
Stack objects die when the function they are created in finishes (strictly speaking they die when the scope whithin they were created in finishes), so you see the destructor being called as a result of that; i.e. when addEmployee finishes the destructor gets called.
The STL list will create a copy of your stack object.
I am assuming you dont have neither a copy constructor nor an overloaded = operator in your Employee class, so the copy STL is doing is a bad copy, since you have pointers inside your Employee class, and when you try using them the program crashes because you have already disposed those pointers in the destructor.
So to solve this problem, create a copy constructor and overload the operator= of your Employee class, just make sure to deal with pointers accordingly inside both the copy constructor and the overladed = operator.
OR
have your list use heap objects rather than stack objects and use this: -
myEmployeeList.push_back(new Employee());
-
In the latter case, make sure to free all the pointers inside the list when needed.
by the way, it is not necessary to keep a count of how many employees you have entered into the list, the function count() will give you the current list size; or is it size()? I cant remember :S
Could you please explain briefly what is a copy constructor and why will it help in this case? and what is operator =, how do i overload it? I've tried to use myEmployeeList.push_back(new Employee());
but still can't make it work... i'm so confused now and don't have much time left...
Could you please explain briefly what is a copy constructor and why will it help in this case? and what is operator =, how do i overload it? I've tried to use myEmployeeList.push_back(new Employee());
but still can't make it work... i'm so confused now and don't have much time left...
I would like to help but seems like doing so properly would require to take a look to many of your classes. I am going to try to assume a few things to help you: -
// This is a copy constructor, notice you pass an Employee object to
-
// the constructor, the point is making an exact copy
-
Employee :: Employee( const Employee &employee )
-
{
-
FirstName = employee.FirstName; // I assume this is a string object
-
Surname = employee.Surname; // I ssume this is a string object too
-
SocialNumber = employee.SocialNumber;
-
Salary = employee.Salary;
-
-
// this is where things become icky, you now must have
-
// copy constructors for clsDate, Address, and Account
-
-
DOB = new clsDate( *employee.DOB );
-
EmploymentStartDate = new clsDate( *employee.EmploymentStartDate );
-
-
HomeAddress = new Address( *employee.HomeAddress );
-
-
theAccounts = new Account*[3];
-
theAccounts[0] = new CurrentAccount( *employee.theAccounts[0] );
-
}
-
// this is the operator= it is used heavily by STLs
-
Employee & Employee::operator=( const Employee &employee )
-
{
-
if(this==&employee)
-
return *this;
-
-
delete HomeAddress;
-
HomeAddress = NULL;
-
-
-
delete DOB;
-
delete EmploymentStartDate;
-
-
delete theAccounts[0];
-
delete theAccounts;
-
-
FirstName = employee.FirstName; // I assume this is a string object
-
Surname = employee.Surname; // I ssume this is a string object too
-
SocialNumber = employee.SocialNumber;
-
Salary = employee.Salary;
-
-
// this is where things become icky, you now must have
-
// copy constructors for clsDate, Address, and Account
-
-
DOB = new clsDate( *employee.DOB );
-
EmploymentStartDate = new clsDate( *employee.EmploymentStartDate );
-
-
HomeAddress = new Address( *employee.HomeAddress );
-
-
theAccounts = new Account*[3];
-
theAccounts[0] = new CurrentAccount( *employee.theAccounts[0] );
-
return *this;
-
}
-
As you can see now you need copy constructors for Address, clsDate and CurrentAccount.
If you are in a hurry use a list of heap objects.
list< *Employee > instead of
list< Employee >
so you would avoid all this copy constructor thing.
Regards
If you are in a hurry use a list of heap objects.
list< *Employee > instead of
list< Employee >
so you would avoid all this copy constructor thing.
Regards
Thank you very much for help, i'll try to study copy constructor thing when i get a bit of time.
i'm using vectors in this case, would
vector<*Employee>
mean that i create a vector of pointers to Employee objects?
Thank you very much for help, i'll try to study copy constructor thing when i get a bit of time.
i'm using vectors in this case, would
vector<*Employee>
mean that i create a vector of pointers to Employee objects?
Correct.
As a side note, you would need to take a look into getEmployee when doing that change.
void EmployeeList :: addEmployee(int &i, int &SIZE)
{
i = SIZE;
myEmployeeList.push_back(Employee()); <<<<<<
SIZE++;
ListSize = SIZE;
}
Why are you calling constructors???? There is no object.
You should have:
[code=cpp]
Employee temp;
myEmployeeList.push_back(temp);
[/quote]
The Employee() creates a temporary object that is copied to the vector.
I assume you have a proper copy constructor and aren't doing something foolish like copying pointers. If you are, when the temporary object is deleted and the destructor called, the data for the object in the vector will be deleted also. Be sure your copy constrcutor actually makes a copy of the data members.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ram |
last post by:
Hi All,
I am using Object Data Source to bind data in the gridview. I have
set the property AllowSorting=true. While running the application, I
could sort the data only in ascending order. Is...
|
by: bxstylez |
last post by:
rewrite this program (sum,average) using functions, possibly arrays?
#include<stdlib.h>
#include<stdio.h>
main()
{
FILE *finpt,
*foutpt;
|
by: MiM22 |
last post by:
first thanks for every one who helps me...
guys..I have to do program that count the value of Pi.
where
pi= 4( 1-(1/3)+(1/5)-(1/7)+((-1)^n/(2n+1))
I did the program but I have problems...
|
by: sajithamol |
last post by:
I have a dll of a VB application which performs image manipulations. I want to build this dll file into a .cab file to embed this in the Object tag in client side. Also how to generate the classid...
|
by: fatin hhmed |
last post by:
How to program RSA algorithms using python language includes the public and private key, encryption and decryption
|
by: Duggi |
last post by:
I used to wonder why MS implemented C# to accept the following code
using (Font f = new Font())
{
// some code here.
}
While the same can be achieved through
{
|
by: Duggi |
last post by:
I used to wonder why MS implemented C# to accept the following code
using (Font f = new Font())
{
// some code here.
}
While the same can be achieved through
{
|
by: progvar |
last post by:
hi
i am getting probleum in converting a spell check progrma made in vb 6.0 and Ms-office Word into OpenOffice Word
when i made spell check program in Ms-Word and vb 6.0 it works fine
but i use...
|
by: raana syeda |
last post by:
Please tell me how to get the Windows Xp program file path using java as I am using java1.6.0
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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: 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...
| |