[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Adding TMenuItem at run time...

Posted on 2000-01-18
7
Medium Priority
?
443 Views
Last Modified: 2010-04-06
Dear Friends,

First of all, happy new year and and lot of success for 2000 to all of you...

I'm writing a component (like a TreeView) and I need to assign a popup menu to it.
At run time, everything is working fine as expected but when I close the application, I
get the following error:

  raised exception class EExternalException...

If I remove the two lines (see code sample below)

  PopupMenu.Items.Add(MnAddGroup);
  PopupMenu.Items.Add(MnAddProject);

I didn't receive this error anymore but, of course, the popup menu is not more dispayed.

What's wrong ?
Thank you in advance for your help
Bob

{------------------------------------------------------------------------------}
type

  TRBRepositoryTree = class
  private
    { Private declarations }
    MyPopup     : TPopupMenu;
    MnAddGroup  : TMenuItem;
    MnAddProject: TMenuItem;

    ...

  public
    { Public declarations }
    Constructor   Create(AOwner: TComponent); override;
    Destructor    Destroy; override;

    ...

  end;
{------------------------------------------------------------------------------}
Constructor TRBRepositoryTree.Create(AOwner: TComponent);
var
  MyBitmap: TBitmap;
begin
  Inherited Create(AOwner);

  {Popup Menu related actions}
  MyPopup               := TPopUpMenu.Create(self);
  PopupMenu.AutoPopup   := true;
 
  MnAddGroup            := TMenuItem.Create(self);
  MnAddGroup.Caption    := 'Add Group...';
  MnAddGroup.OnClick    := NewGroupClick;

  MnAddProject          := TMenuItem.Create(self);;
  MnAddProject.Caption  := 'Add Project...';
  MnAddProject.OnClick  := NewProjectClick;

  PopupMenu:= PopupMenu;

  PopupMenu.Items.Add(MnAddGroup);
  PopupMenu.Items.Add(MnAddProject);
 
  ...

end;
{------------------------------------------------------------------------------}
Destructor TRBRepositoryTree.Destroy;
begin
  ImagLst.Free;
  PopupMenu.Free;
  MnAddGroup.Free;
  MnAddProject.Free;
  Inherited Destroy;
end;
{------------------------------------------------------------------------------}

0
Comment
Question by:baudewyns
  • 4
  • 2
7 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 total points
ID: 2365848
hi baudewyns,

just before i do a closer look, a guess

Destructor TRBRepositoryTree.Destroy;
begin
  ImagLst.Free;
  PopupMenu.Free;
  MnAddGroup.Free;   //must not freed, freed by popupmenu or at least by self
  MnAddProject.Free;    //must not freed, freed by popupmenu or at least by self
  Inherited Destroy;
end;

well, just a guess (not tested).
can you specify, at which line/procedure the exception is raised?

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2365866
hi again,

i guess i got it
replace this line
PopupMenu:= PopupMenu;
with
PopupMenu:= MyPopup;
in your create-statement

meikl
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2366096
The Free calls are the cause for the exception. The menu items are created with an owner (whichever it is, doesn't matter). This owner will release the classes on its own destruction. Hence the variables for MnAddGroup etc. aren't valid anymore (more correct, the inherited destructor tries to free already freed classes).

To avoid further problems with menus I'd like to suggest that you look closer at NewItem. This will create menu items and assign all necessary data (caption, event etc.) in one call. You can directly use the result of NewItem in the Items.Add command so no intermediate references are kept if you don't need them.

Ciao, Mike
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2366123
hi mike,

yup, this was also my first look, but
it was telled if removed the add-method no exception is raised.

the realy problem lies also here:

  PopupMenu:= PopupMenu; //thats nil := nil

  PopupMenu.Items.Add(MnAddGroup); //thats Nil.Items...->Exception
   
meikl
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2366185
That's what I thought too, but I think this is only a typo from stripping down the code for sending it here, because if this would really be so in code then the program wouldn't even have started without throwing an exception, but the question above states that the program runs fine until it is stopped.

Ciao, Mike
0
 

Author Comment

by:baudewyns
ID: 2369868
Here are the conclusion:

* The code "PopupMenu:= PopupMenu;" was only a bad retranscription of my own code which was correctly "PopupMenu:= MyPopup" as expected.

* If I remove the Free method for the two MenuItem, I don't receive the error anymore as suggested by meikl.
Conclusion, if I create the object without using it, I can free myself the object. If I assign this object to another one, this job is done automatically by this parent object.

Thank you all, that was very simple as usual.
 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2369910
hi baudewyns,

glad thats now working,
decide now, who should be graded,
because also mike's suggestion was correct and more detailed as my was.

just use the link accept comment as answer, which should available upper right to each comment.

don't worry to decide to grade mike.
it's your decission.

meikl
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question