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
loostAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi loost,

? 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]) then
    begin
      items.Objects[i].Free;
      items.Objects[i] := nil;
    end;
  end;
  inherited Destroy;
end;

meikl
0
loostAuthor Commented:
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(newString: string);
begin
  inherited Create;
  id := newString;
end;

TObject doesn't have and isn't concerned about a parent property, or is it?
0
kretzschmarCommented:
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
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

loostAuthor Commented:
> 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;
0
kretzschmarCommented:
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
0
loostAuthor Commented:
Adjusted points to 50
0
loostAuthor Commented:
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?
0
kretzschmarCommented:
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
0
pivarCommented:
Hi,

I think the problem is that the windows combobox is destroyed before the TMyComboBox.Destroy is run. So when you access the Items in the destructor the Items are, as you say, already dereferenced.

One solution would be to use WndProc to capture the WM_DESTROY message instead of Destroy:

procedure TMyComboBox.WndProc(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;
  inherited;
end;

The problem with this approach is that you get a WM_DESTROY everytime you change the Style property.

Anyway, you have to free the Items before Destroy.

/pivar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
good point, pivar,
could really be ;-)
0
loostAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.