473,385 Members | 1,834 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,385 software developers and data experts.

Access violation

62
Hello,
I am working on a program with several classes. It is for storing employee data.
I enter all data in the edit boxes in my form, then press a button create new employee and it should store this data in the memory and display in labels.
The problem is i am getting an access violation exception at inputting the data into one of the objects.
Here is the function for the button:
Expand|Select|Wrap|Line Numbers
  1. void __fastcall TForm1::CreateEmployeeButtonClick(TObject *Sender)
  2. {
  3.         theEmployeeList.addEmployee(i, Size);
  4.  
  5.         theEmployeeList.getEmployee(i)->create(EnterEmployeeName,
  6.                                               EnterEmployeeSurname,
  7.                                               EnterEmployeeSocialNumber,
  8.                                               EnterEmployeeSalary,
  9.                                               EnterHouseName,
  10.                                               EnterHouseNo,
  11.                                               EnterStreet,
  12.                                               EnterTown,
  13.                                               EnterPostCode,
  14.                                               EnterCountry,
  15.                                               EnterDOBdate,
  16.                                                 EnterDOBmonth,
  17.                                                 EnterDOByear,
  18.                                                 EnterEmploymentStartDate,
  19.                                                 EnterEmploymentStartMonth,
  20.                                                 EnterEmploymentStartYear,
  21.                                                 EnterBankCode,
  22.                                                 EnterAccountNo,
  23.                                                 EnterBalance,
  24.                                                 EnterBankName,
  25.                                                 EnterRate,
  26.                                                 EnterOverdraftLimit,
  27.                                                 EnterConditions);
  28.  
  29.         theEmployeeList.getEmployee(i)->display(NameField,
  30.                                               SurnameField,
  31.                                               SocialNumberField,
  32.                                               SalaryField,
  33.                                               AddressMemo,
  34.                                               DOBLabel,
  35.                                               EmploymentStartDateLabel,
  36.                                               BankCodeLabel,
  37.                                               AccountNoLabel,
  38.                                               BalanceLabel,
  39.                                               BankNameLabel,
  40.                                               RateLabel,
  41.                                               OverdraftLimitLabel,
  42.                                               ConditionsLabel);
  43.  
  44. }
  45.  
The addEmployee function:

Expand|Select|Wrap|Line Numbers
  1. void EmployeeList :: addEmployee(int &i, int &SIZE)
  2. {
  3.         i = SIZE;
  4.         myEmployeeList.push_back(Employee());
  5.         SIZE++;
  6.         ListSize = SIZE;
  7. }
  8.  
The create function for employee:
Expand|Select|Wrap|Line Numbers
  1. void Employee :: create(TEdit *EnterEmployeeName,
  2.                         TEdit *EnterEmployeeSurname,
  3.                         TEdit *EnterEmployeeSocialNumber,
  4.                         TEdit *EnterEmployeeSalary,
  5.                         TEdit *EnterEmployeeHouseName,
  6.                         TEdit *EnterEmployeeHouseNo,
  7.                         TEdit *EnterEmployeeStreet,
  8.                         TEdit *EnterEmployeeTown,
  9.                         TEdit *EnterEmployeePostCode,
  10.                         TEdit *EnterEmployeeCountry,
  11.                         TEdit *EnterDOBdate,
  12.                         TEdit *EnterDOBmonth,
  13.                         TEdit *EnterDOByear,
  14.                         TEdit *EnterEmploymentStartDate,
  15.                         TEdit *EnterEmploymentStartMonth,
  16.                         TEdit *EnterEmploymentStartYear,
  17.                          TEdit *EnterBankCode,
  18.                          TEdit *EnterAccountNo,
  19.                          TEdit *EnterBalance,
  20.                          TEdit *EnterBankName,
  21.                          TEdit *EnterRate,
  22.                          TEdit *EnterOverdraftLimit,
  23.                          TEdit *EnterConditions)
  24. {
  25.         //save data for the employee
  26.         FirstName = EnterEmployeeName->Text;
  27.         Surname = EnterEmployeeSurname->Text;
  28.         Salary = StrToInt(EnterEmployeeSalary->Text);
  29.         SocialNumber = StrToFloat(EnterEmployeeSocialNumber->Text);
  30.  
  31.         HomeAddress -> create(EnterEmployeeHouseName->Text,
  32.                                 StrToInt(EnterEmployeeHouseNo->Text),
  33.                                 EnterEmployeeStreet->Text,
  34.                                 EnterEmployeeTown->Text,
  35.                                 EnterEmployeePostCode->Text,
  36.                                 EnterEmployeeCountry->Text);
  37. .....
  38. }
  39.  
  40.  
And the create function for the HomeAddress pointer, this is basically where i get the exception, after it inputs all data, on the closing bracket.
Expand|Select|Wrap|Line Numbers
  1. void Address :: create(String aHouseName, int aHouseNumber, String aStreet,
  2.              String aTown, String aPostcode,  String aCountry)
  3. {
  4.         Street = aStreet;
  5.         HouseNo = aHouseNumber;
  6.         HouseName = aHouseName;
  7.         PostCode = aPostcode;
  8.         Town = aTown;
  9.         Country = aCountry;
  10.  
  11. }
  12.  
I was thinking that might do with the constructors and memory allocation, but it all seems correct.
This is where i allocate the memory for address:
Expand|Select|Wrap|Line Numbers
  1. Employee :: Employee()
  2. {
  3.         FirstName = "";
  4.         Surname = "";
  5.         SocialNumber = -1;
  6.         Salary = -1;
  7.         DOB = new clsDate();
  8.         EmploymentStartDate = new clsDate();
  9.         myCurrentAccount = new CurrentAccount();
  10.  
  11.         HomeAddress = new Address();
  12. }
  13.  
I've been getting this exception for other program as well and can't figure out what i'm doing wrong.
Would be very grateful if anyone has advice.
Nov 14 '07 #1
1 1382
weaknessforcats
9,208 Expert Mod 8TB
Employee :: Employee()
{
FirstName = "";
Surname = "";
SocialNumber = -1;
Salary = -1;
DOB = new clsDate();
EmploymentStartDate = new clsDate();
myCurrentAccount = new CurrentAccount();

HomeAddress = new Address();
}
Part of yourt problem is here. FirstName is set to the address of a "" string. If you ever try to chnage FirstName, you will crash with a segmentation fault.

You have to:
Expand|Select|Wrap|Line Numbers
  1. FirstName = new char[1];
  2. FirstName[0] = '\0';
  3.  
Now you have a null string in FirstName that can be deleted.

Also you see me be calling constructors instead of creating objects.
HomeAddress = new Address();
This shouls be:
Expand|Select|Wrap|Line Numbers
  1. HomeAddress = new Address;
  2.  
I would make changes thoughout tyour code that a) never assigns a literal to a field you may change, b) create objects and do not call constructors.

I don't see you class but it should also have a destructor and a copy constructor.
Nov 15 '07 #2

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

Similar topics

15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
5
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
7
by: Daniel | last post by:
I want to write a method to remove the last node of the linked list. But the error "Access Violation" exists and the error point to this method. What does it means by Access Violation and how can...
0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
1
by: Thomas Albrecht | last post by:
My application fails during initialization of the dlls with an ExecutionEngineException and a access violation in the MFC app. The structure of the program looks like: MFC app -> mixed DLL ->...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
2
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol...
2
by: =?Utf-8?B?c29jYXRvYQ==?= | last post by:
Hi, I have a DLL in VC6, when a specific function is called it will spawns a few threads and then return. The threads stay running and inside one of these threads an event is created using the...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
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
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...
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
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,...

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.