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(cla ss 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:
And this is the function in my Form where i call this Employee class function create(), which is preformed after the destructor...
Could someone please help...
I have a program with several classes which i was debugging and just found out strange thing: it creates the object in a constructor(cla ss 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:
Code:
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; }
Code:
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...
Comment