Board index » cppbuilder » Nested TList crashes

Nested TList crashes


2007-01-02 03:07:51 PM
cppbuilder12
I'm attempting to create a TList within another TList but when I attempt
to add an item to the inner TList the program crashes with
EAccessViolation "Access violation at address 0042FB2F. Read of address
00000026". I bet I'm missing something obvious but it's stumping me. I
hit archives of these newsgroups and google, best I found was delphi
discussions on using nested TList (I don't know delphi though) and I
assume it's doable in bcb too.
I was able to setup a short example below (I'm aware it leaks memory).
I'm using BCB 5.
struct TDevice_Info {
String ID;
};
struct TClass_Info {
String ClassName;
TList *Devices;
};
//Structure wanted:
// DeviceList
// +-- Class1
// | +--- Device1
// | +--- Device2
// +--- Class2
/// ...etc
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{
TList *DeviceList = new TList; //Create a List of Devices
TClass_Info *Class = new TClass_Info; //Create a Class item
Class->ClassName = "Classname Test";
TDevice_Info *Device = new TDevice_Info; //Create a Device item
Device->ID = "Device ID Test";
Class->Devices->Add(Device); //Add device item to the class's tlist
DeviceList->Add(Class); //Add class to the devicelist tlist
}
The program crashes at the "Class->Devices->Add(Device);" line. If I
remove that line it runs without crashing. Thanks for any clues.
 
 

Re:Nested TList crashes

(I originally posted this in vcl.using but found that bcb classes like
TList should be posted here instead)
I'm attempting to create a TList within another TList but when I attempt
to add an item to the inner TList the program crashes with
EAccessViolation "Access violation at address 0042FB2F. Read of address
00000026". I bet I'm missing something obvious but it's stumping me. I
hit archives of these newsgroups and google, best I found was delphi
discussions on using nested TList (I don't know delphi though) and I
assume it's doable in bcb too.
I was able to setup a short example below (I'm aware it leaks memory).
I'm using BCB 5.
struct TDevice_Info {
String ID;
};
struct TClass_Info {
String ClassName;
TList *Devices;
};
//Structure wanted:
// DeviceList
// +-- Class1
// | +--- Device1
// | +--- Device2
// +--- Class2
/// ...etc
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{
TList *DeviceList = new TList; //Create a List of Devices
TClass_Info *Class = new TClass_Info; //Create a Class item
Class->ClassName = "Classname Test";
TDevice_Info *Device = new TDevice_Info; //Create a Device item
Device->ID = "Device ID Test";
Class->Devices->Add(Device); //Add device item to the class's tlist
DeviceList->Add(Class); //Add class to the devicelist tlist
}
The program crashes at the "Class->Devices->Add(Device);" line. If I
remove that line it runs without crashing. Thanks for any clues.
 

Re:Nested TList crashes

Quote
I'm attempting to create a TList within another TList but when I attempt
to add an item to the inner TList the program crashes with
EAccessViolation "Access violation at address 0042FB2F. Read of address
00000026".
Sorry to reply to myself. I forgot to actually create the inner object
that the inside Devices TList uses. So it should be like so:
struct TDevice_Info {
String ID;
};
struct TClass_Info {
String ClassName;
TList *Devices;
};
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{
TList *DeviceList = new TList; //Create a List of Devices
TClass_Info *Class = new TClass_Info; //Create a Class item
Class->ClassName = "Classname Test";
Class->Devices = new TList; //<---- CREATE THE CLASSES TLIST FIRST!
TDevice_Info *Device = new TDevice_Info; //Create a Device item
Device->ID = "Device ID Test";
Class->Devices->Add(Device); //Add device item to the class's tlist
DeviceList->Add(Class); //Add class to the devicelist tlist
}
So breaking it down:
1. Create a TList for DeviceList
2. Create Class which points to a TClass_Info structure
3. Create a TList for Class->Devices
4. Create Device which points to a TDevice_Info structure
5. Add the Device (created in #4) to Class->Devices (created in #3)
6. Add the Class (created in #2, assinged in #3 thru #4) into DeviceList
(created in #1).
This seems to be a bit messy but works. I was attempting to do this as a
replacement of what I was doing previously with plain structures but had
a fixed sized array and I needed something more dynamic. Any one have
another suggestion on perhaps another way of doing something similar? I
gave std::list and std::map a shot but it didn't seem any easier. I also
plan to work with this structure as a whole inside a thread (after
switching to TThreadList). Thanks for any clues.
 

{smallsort}