0

我有一个 C++Builder VCL 应用程序,上面有一些动态创建的组件。这些组件应该能够从面板停靠和取消停靠。

问题是,如果用户在这些组件之一未停靠(浮动)时关闭表单,我会收到错误消息:

第一次机会例外,价格为 $50C278EF。异常类 $C0000005 带有消息“在 0x50c278ef 处的访问冲突:读取地址 0x00000010”。处理 Project1.exe (8004)

这是一个演示问题的简化示例:

HPP:

//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TPanel *Panel1;
    TPanel *Panel2;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
private:    // User declarations
    TPanel *Panel3;
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

共产党

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    Panel3 = new TPanel((TComponent*)nullptr);
    Panel3->Parent = Panel1;
    Panel3->Width = 50;
    Panel3->Height = 50;
    Panel3->DragKind = dkDock;
    Panel3->DragMode = dmAutomatic;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    delete Panel3;
}
//---------------------------------------------------------------------------

DFM

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 128
    Top = 48
    Width = 409
    Height = 217
    Caption = 'Panel1'
    DockSite = True
    TabOrder = 0
    object Panel2: TPanel
      Left = 216
      Top = 8
      Width = 185
      Height = 97
      Caption = 'Panel2'
      DragKind = dkDock
      DragMode = dmAutomatic
      TabOrder = 0
    end
  end
end

在这个例子中,如果Panel3是 Floating 并且用户关闭了那个表单,我们就会得到异常。

4

0 回答 0