Re: Thread Question - part 2


2003-06-29 12:38:50 AM
cppbuilder84
I think I found out what my problem was, but I don't understand why.
From my component I have in its constructor
__fastcall TMyComponent::TMyComponent(TComponent* Owner) : TComponent(Owner)
{
FClass1 = new TMyClass1();
FClass2 = new TMyClass2();
FClass1->OnGotFileList = OnGotFileList1Event;
FClass2->OnGotFileList = OnGotFileList2Event;
}
// -------------------------
this will give me an error when it goes to the FClass2 event (EAccess
Violation), the error comes from the FClass2 event when it tries to call the
event which is pointing back to the Components event
if(OnGotFileList) // this should be going back to the component's event
if setup
OnGotFileList(this, FileList);
// -------------------------
void __fastcall FClass1::OnGotFileListEvent(TObject *Sender, TStrings* List)
{ // list is passed from the thread before ending, copy its contents
FileList->Clear();
FileList->AddStrings(List);
ShowMessage("FClass1->OnGotFileListEvent()"); // this always gets
displayed
if(OnGotFileList) // this should be going back to the component's event
if setup
OnGotFileList(this, FileList);
}
void __fastcall FClass2::OnGotFileListEvent(TObject *Sender, TStrings* List)
{ // list is passed from the thread before ending, copy its contents
FileList->Clear();
FileList->AddStrings(List);
ShowMessage("FClass2->OnGotFileListEvent()"); // this always gets
displayed
if(OnGotFileList) // this should be going back to the component's event
if setup
OnGotFileList(this, FileList); // the error is somewhere here i
think!!
}
// -------------------------
void __fastcall TMyComponent::OnGotFileList1Event(TObject *Sender, TStrings*
List)
{
ShowMessage("TMyComponent->OnGetFileList1Event()"); // this always gets
displayed
}
void __fastcall TMyComponent::OnGotFileList2Event(TObject *Sender, TStrings*
List)
{
ShowMessage("TMyComponent->OnGetFileList2Event()"); // I never get to
see this
}
// -------------------------
now, If i take the
FClass2->OnGotFileList = OnGotFileList2Event;
out of the TMyComponent's constructor and put it into the Forms'1
constructor like below I don't get the access violation error anymore
// -------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
MyComponent1 = new TMyComponent(this);
MyComponent1->FClass2->OnGotFileList =
MyComponent1->OnGotFileList2Event;
}
// -------------------------
but it works either way for FClass1 which does more or less the same as
FClass2, only it has more functions in it and an extra TString. If I add the
extra TString to the FClass2 in the header file I can call it anything I
want, like TStrings* AListThatsNeverUsed; and It will work, take it out and
it won't. The TString's never gets used whatsoever in any of the files.
Does anyone have some input with regards to this?
Thanks for your time,
MwnN.