将值分配给我的组件位图属性时访问冲突 [英] Access violation when assigning a value to my component Bitmap property
问题描述
我正在尝试创建一个必须使用位图的组件,当我在属性上选择图像时遇到问题.
I'm trying to create a component that must use a Bitmap, I'm having a problem when I go to select the image on the property.
以下是代码的摘录:财产声明
Here is an excerpt of the code: Property Declaration
Property StarOff: TBitmap read FStarOff write SetStarOff;
功能设置关闭
procedure TNotas.SetStarOff(const Value: TBitmap);
begin
FStarOff.Assign(Value);
end;
但是,当我为属性赋值时,我得到了错误:
But, when I assign a value to the property, I get the error:
模块TNte.bpl"中地址 1BC324B8 的访问冲突.读取地址 000000000
Access violation at address 1BC324B8 in module 'TNte.bpl'. Read of address 000000000
为什么会这样?
推荐答案
你的 setter 方法看起来是正确的,但是你得到了访问冲突,因为你的 FStarOff 成员现在是 nil.
Your setter method looks correct, but you're getting an Access Violation because your FStarOff member is nil at the moment.
通常是在构建时创建它并在销毁时释放它.
The usual is to create it at construction time and free it at destruction time.
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited;
FStarOff := TBitmap.Create;
end;
destructor TMyComponent.Destroy;
begin
FStarOff.Free;
inherited;
end;
这篇关于将值分配给我的组件位图属性时访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!