BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Nested TList crashes

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language)
View previous topic :: View next topic  
Author Message
catfish
Guest





PostPosted: Wed Jan 03, 2007 1:27 am    Post subject: Nested TList crashes Reply with quote



(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.
Back to top
catfish
Guest





PostPosted: Wed Jan 03, 2007 6:14 am    Post subject: Re: Nested TList crashes Reply with quote



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.
Back to top
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group
.