Hi,
This one is really starting to drive me to pulling all my hair out!
I want to build a vector of objects and then use the collection to populate rows of a table. I don't know how many instances there will be so I thought the flexability of the size of vectors was the answer.
However I am having issues with the memory allocation resulting in corrupt data, access violation errors and invalid pointer errors, but not all the time sometimes it works fine!?!
This code I am using is:
The class TDataClass just consists of some private properties that are made up of Strings, Dates, and floats, and the public Gets and Sets needed to manage them. It does have a constructor that initialises all the values to empty strings or zero, I didn't have this to start with and added it while trying to fix this problem - it doesn't seem to have have change the behaviour of anything.
The problem!: Sometimes this works great, other times the data in the cells of the table is corrupted or missing, most often it fails at the
line with either an access violation or an invalid pointer error.
I have tried using a String value in place of the TDataClass object, the only diffence in the code being the use of
to get the data out and it works perfectly so it is something to do with using an object and/or the way I'm handling the pointers.
I have now run out of ideas, and wondered if anyone could help, or point (no pun intended!) me in the right direction.
Thanks.
This one is really starting to drive me to pulling all my hair out!
I want to build a vector of objects and then use the collection to populate rows of a table. I don't know how many instances there will be so I thought the flexability of the size of vectors was the answer.
However I am having issues with the memory allocation resulting in corrupt data, access violation errors and invalid pointer errors, but not all the time sometimes it works fine!?!
This code I am using is:
Code:
. //This part of the code is where a screen is called to allow user to enter data.
. //They can do this as often as they want which is why I'm using a vector.
.
TDataEntryForm* dataForm = new TDataEntryForm(aParam); //Just a form so user can enter data
TDataClass *record = new TDataClass(); //Data object to hold details entered.
dataForm->CreateRecord(record); //Pass data obj by ref to form to be populated. This method displays the form allowing the user to enter data and populates the data obj and closes the form on an ok click.
myVector.push_back(*record); //Add actual data object to vector. (This is declared as vector<TDataClass> myVector in header)
delete record; //clean up
record = NULL;
delete dataForm;
dataForm = NULL;
.
.
.
. //This part of the code populates the table on the screen with the data from the vector (if it contains any.)
.
.
vector<TDataClass>::iterator i;
for(i = myVector.begin(); i != myVector.end(); i++)
{
TDataClass* record = new TDataClass();
record = i; //get the data out of the vector.
.
. //Data population code using get methods of TDataClass - i.e. record->GetId();
.
delete record; //clean up
record = NULL;
}
.
.
.
The class TDataClass just consists of some private properties that are made up of Strings, Dates, and floats, and the public Gets and Sets needed to manage them. It does have a constructor that initialises all the values to empty strings or zero, I didn't have this to start with and added it while trying to fix this problem - it doesn't seem to have have change the behaviour of anything.
The problem!: Sometimes this works great, other times the data in the cells of the table is corrupted or missing, most often it fails at the
Code:
myVector.push_back(*record);
I have tried using a String value in place of the TDataClass object, the only diffence in the code being the use of
Code:
String record = *i;
I have now run out of ideas, and wondered if anyone could help, or point (no pun intended!) me in the right direction.
Thanks.