loost
asked on
Freeing item objects in decendant combo
Hi
I created a component decending from TComboBox that manipulates the items.objects property within itself.
When I try to free the objects, which I added with AddObject, in MyComboBox.Destroy I get the following error:
Project myproject.exe raised exception class EInvalidOperation with message 'Control '' has no parent window'. Process stopped ...
This is my destructor for the Combo decendant:
destructor TMyComboBox.Destroy;
var i: integer;
begin
for i := 0 to (Items.Count - 1) do
begin
if Assigned(items.Objects[i]) then
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
inherited Destroy;
end;
How can I avoid the exception from occuring and still free the objects attached to the combo's items?
Thanks
I created a component decending from TComboBox that manipulates the items.objects property within itself.
When I try to free the objects, which I added with AddObject, in MyComboBox.Destroy I get the following error:
Project myproject.exe raised exception class EInvalidOperation with message 'Control '' has no parent window'. Process stopped ...
This is my destructor for the Combo decendant:
destructor TMyComboBox.Destroy;
var i: integer;
begin
for i := 0 to (Items.Count - 1) do
begin
if Assigned(items.Objects[i])
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
inherited Destroy;
end;
How can I avoid the exception from occuring and still free the objects attached to the combo's items?
Thanks
ASKER
RE what do you add with addobject ?
RE how do you create the objects, which you have added ?
The idea is to make a lookup that have an easily accesible key value representing a descriptive value that is displayed to the user.
The following adds an item:
procedure TMyComboBox.AddItem(const Description: string; const Key: string);
begin
items.AddObject( Description, TStringIdObject.Create(Key ) );
end;
TStringIdObject is a simple wrapper for storing the key:
TStringIdObject = class(TObject)
id: string;
constructor create(newString: string);
end;
constructor TStringIdObject.create(new String: string);
begin
inherited Create;
id := newString;
end;
TObject doesn't have and isn't concerned about a parent property, or is it?
RE how do you create the objects, which you have added ?
The idea is to make a lookup that have an easily accesible key value representing a descriptive value that is displayed to the user.
The following adds an item:
procedure TMyComboBox.AddItem(const Description: string; const Key: string);
begin
items.AddObject( Description, TStringIdObject.Create(Key
end;
TStringIdObject is a simple wrapper for storing the key:
TStringIdObject = class(TObject)
id: string;
constructor create(newString: string);
end;
constructor TStringIdObject.create(new
begin
inherited Create;
id := newString;
end;
TObject doesn't have and isn't concerned about a parent property, or is it?
hmm loost,
yup, a tobject or descended objects cannot, i guess, raise this exception.
must be a subclass from tcontrol.
are you sure that the errormessage comes up during the for-loop ?
or is just the parent of your descending combobox freed before freeing the combobox, so that the combobox have no parent ?
meikl
yup, a tobject or descended objects cannot, i guess, raise this exception.
must be a subclass from tcontrol.
are you sure that the errormessage comes up during the for-loop ?
or is just the parent of your descending combobox freed before freeing the combobox, so that the combobox have no parent ?
meikl
ASKER
> are you sure that the errormessage comes up during the for-loop ?
The short answer: No
If I remove the for loop and my destructor only contains:
inherited Destroy;
then I don't get an exception, but my StringId objects aren't freed either.
> or is just the parent of your descending combobox freed before freeing the combobox
The exception actually does complain about my combobox parent not being there.
As a simple test I basically:
registered the component
created a new app with one form
put one of my combo's on it
When I run and then exit that form the exception occurs.
How can my combo's parent then be freed before the parent itself. Isn't it standard procedure to do it the other way around? My combo is allmost identical to Delphi's native combo? My constructor only contains:
inherited Create(AOwner);
I really thought this to be a very simple (and easy) component and don't understand what can go wrong.
If I add the following code to my form's Onclose event, The combo is freed correctly. This is very ugly though:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var i: integer;
begin
for i := (ComponentCount - 1) downto 0 do
if components[i] is TMyComboBox then
TMyComboBox(components[i]) .Free;
end;
The short answer: No
If I remove the for loop and my destructor only contains:
inherited Destroy;
then I don't get an exception, but my StringId objects aren't freed either.
> or is just the parent of your descending combobox freed before freeing the combobox
The exception actually does complain about my combobox parent not being there.
As a simple test I basically:
registered the component
created a new app with one form
put one of my combo's on it
When I run and then exit that form the exception occurs.
How can my combo's parent then be freed before the parent itself. Isn't it standard procedure to do it the other way around? My combo is allmost identical to Delphi's native combo? My constructor only contains:
inherited Create(AOwner);
I really thought this to be a very simple (and easy) component and don't understand what can go wrong.
If I add the following code to my form's Onclose event, The combo is freed correctly. This is very ugly though:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var i: integer;
begin
for i := (ComponentCount - 1) downto 0 do
if components[i] is TMyComboBox then
TMyComboBox(components[i])
end;
hi loost,
really mysterious, but
the owner and the parent can be different.
if you will send me your source, then i can take a closer look to it, send it to
meikl@spektracom.de
meikl
really mysterious, but
the owner and the parent can be different.
if you will send me your source, then i can take a closer look to it, send it to
meikl@spektracom.de
meikl
ASKER
Adjusted points to 50
ASKER
Please Help
My problem seems to be that the items property of the combobox is allready dereferenced by the time my combobox's destructor is called below:
destructor TMyComboBox.Destroy;
var i: integer;
begin
for i := (Items.Count - 1) downto 0 do
begin
if Assigned(items.Objects[i]) then
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
inherited Destroy;
end;
It crashes on accessing items.count or any items property for that matter.
How is this possible and how do I get around it?
Any luck yet, Meikl?
My problem seems to be that the items property of the combobox is allready dereferenced by the time my combobox's destructor is called below:
destructor TMyComboBox.Destroy;
var i: integer;
begin
for i := (Items.Count - 1) downto 0 do
begin
if Assigned(items.Objects[i])
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
inherited Destroy;
end;
It crashes on accessing items.count or any items property for that matter.
How is this possible and how do I get around it?
Any luck yet, Meikl?
hi loost,
sorry for delay, but the mail-server from my provider was unaccessable for me (i getted no mails, but sending was ok, a misconfiguration i guess), just received your mail, take a look now to it.
meikl
sorry for delay, but the mail-server from my provider was unaccessable for me (i getted no mails, but sending was ok, a misconfiguration i guess), just received your mail, take a look now to it.
meikl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
good point, pivar,
could really be ;-)
could really be ;-)
ASKER
Thanks pivar
It works with the following slightly modified syntax:
procedure TMyComboBox.WndProc(var Message: TMessage);
var i: integer;
begin
if (Message.Msg = WM_DESTROY) then
begin
for i := (Items.Count - 1) downto 0 do
begin
if Assigned(items.Objects[i]) then
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
end;
inherited;
end;
The style won't be a problem for me, since I change that right after creation and never again.
It works with the following slightly modified syntax:
procedure TMyComboBox.WndProc(var Message: TMessage);
var i: integer;
begin
if (Message.Msg = WM_DESTROY) then
begin
for i := (Items.Count - 1) downto 0 do
begin
if Assigned(items.Objects[i])
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
end;
inherited;
end;
The style won't be a problem for me, since I change that right after creation and never again.
? what do you add with addobject ?
? how do you create the objects, which you have added ?
seems to me that, whatever object it is, it is not created with a parent or the parent is not adjusted after creation.
>How can I avoid the exception from
>occuring and still free the objects
>attached to the combo's items?
1. assign a parent after creation of your added object.
2. maybe this error is caused by an object, which was a child from the currently freed object -> in this case reorder the free method by changing your code like
destructor TMyComboBox.Destroy;
var i: integer;
begin
for i := (Items.Count - 1) downto 0 do
begin
if Assigned(items.Objects[i])
begin
items.Objects[i].Free;
items.Objects[i] := nil;
end;
end;
inherited Destroy;
end;
meikl