Access violation when assigning a value to my component Bitmap property [closed]

南笙酒味 提交于 2020-01-22 03:12:06

问题


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;

Function 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:

Access violation at address 1BC324B8 in module 'TNte.bpl'. Read of address 000000000

Why is this happening?


回答1:


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;


来源:https://stackoverflow.com/questions/13494829/access-violation-when-assigning-a-value-to-my-component-bitmap-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!