源代码如下:
//DLL创建
procedure TYW1Drive.BuildMenu;
var
  NewItem:TMenuItem;
begin
  //其中FormMain是参数传入的;
  FMainMenu:=TMainMenu(FormMain.FindComponent('MainMenu'))
  NewItem := TMenuItem.Create(nil);
  FMainMenu.Items.Items[0].Add(newItem);
  newItem.Caption:='dddd';  NewItem := TMenuItem.Create(nil);
  FMainMenu.Items.Items[0].Add(newItem);
  newItem.Caption:='dddddd';
end;
//DLL退出时释放
for i:=FMainMenu.Items.Items[0].Count-1 downto 0 do
  begin
    MenuItem:= FMainMenu.Items.Items[0].items[I];
    FMainMenu.Items.Items[0].Delete(I);
    if Assigned(MenuItem) then
      MenuItem.Free;
  end;主程序关闭的时候,出现
Application Error
    Exception EAccessViolatio in module PKJ.exe at 00003194
    Access violation at address 00403194 in module 'PKJ.exe' read of address 00FA7940

解决方案 »

  1.   

    for i:=FMainMenu.Items.Items[0].Count-1 downto 0 do
      begin
        MenuItem:= FMainMenu.Items.Items[0].items[I];
        FMainMenu.Items.Items[0].Delete(I);
        if Assigned(MenuItem) then
          MenuItem.Free;
      end;释放的时候,直接释放对象就完了,没有必要一个一个删除item
      

  2.   

    调试的时候是从哪里出错的,这个问题应该是DLL本身的问题,把DLL调试一下,在来发问吧
      

  3.   

    呵呵,又是地址出错的问题,我以前是运行EXE没有问题,但是一做成DLL就出问题了
      

  4.   

    是不是你的DLL在创建的时候又调用了另一个DLL?如果是的吧,很容易就出问题的。建议调试看看
      

  5.   

    没有啊,就是这么一个DLL,而且就这么一个创建主窗口的菜单,不知道为什么释放的时候会报错
      

  6.   

    用freeandnil(MenuItem)试试,用MenuItem.free有时释放不干净
      

  7.   

    library MenuCreate;
    uses
      SysUtils,
      Classes,
      Menus,
      Forms,
      Windows;var  FMainMenu: TMainMenu;
      DLLAPP: TApplication;
      DllScr: TScreen;{$R *.res}procedure BuildMenu(aForm: TForm; App: TApplication; src: TScreen);stdcall;
    var
      NewItem:TMenuItem;
    begin
      Application := App;
      Screen := src;
      
      FMainMenu := TMainMenu(aForm.FindComponent('MainMenu'));
      NewItem := TMenuItem.Create(nil);
      newItem.Caption:='测试';
      FMainMenu.Items.Add(newItem);
      NewItem := TMenuItem.Create(nil);
      newItem.Caption:='dddddd';
      FMainMenu.Items.Items[0].Add(newItem);end;procedure DLL_UnLoadProc(Reason: Integer); register;
    begin
      if Reason = DLL_PROCESS_DETACH then
      begin
        Application := DLLAPP;
        Screen := DllScr;
        FMainMenu.Free;
      end;
    end;exports
      BuildMenu name 'BuildMenu';begin
      DLLAPP := Application;
      DllScr := Screen; 
      DllProc := @DLL_UnLoadProc;end.
    我做了测试没有问题,有问题联系我[email protected]
      

  8.   

    给一方案:将设计后的菜单定义写入文件A,退出Dll后主文件根据A重建菜单。