delphi 10 seattle "Automatic Reference Counting"问题

测试Embarcadero® Delphi 10 Seattle Version 23.0.20618.2753,Multi-Device Application。

从资源中读取图片:

procedure TKsColorBall.GetImagesFromResource;
var
  i:integer;
  Bmp:TBitmap;   
  s:string;
  RS:TResourceStream;
begin
  for i:=IconList.Count-1 downto 0 do
    TObject(IconList.Items[i]).Free;
  IconList.Clear;

  for i:=1 to FOption.IconKind do
  begin
    Bmp:=TBitmap.Create;
    s:=Char(Ord('A')+i-1)+FOption.IconSystem;
    RS := TResourceStream.Create(hInstance, s, RT_RCDATA);
    Bmp.LoadFromStream(RS);
    RS.Free;
    IconList.Add(Bmp);
    Bmp:=nil;
  end;
end;
然后,其它地方从IconList列表中取出来绘图:

procedure TKsColorBall.New3Ball;
var
  Blanks:TStringList;

  procedure PickOutBlank;
  var
    i,j:integer;
  begin
    Blanks.Clear;
    for i:=1 to GapCount do
    for j:=1 to GapCount do if BallList[i,j].Color=-1 then
    begin
      Blanks.Add(InttoStr(i)+','+InttoStr(j));
    end;
  end;
var
  Bmp:TBitmap;
  Row,Col,BallIndex,ColorIndex:integer;
  i,j,n:integer;
begin
  Blanks:=TStringList.Create;
  try
    PickOutBlank;
    if Blanks.Count=0 then
    begin
      if Assigned(FOnGameOver) then FOnGameOver(self);
      exit;
    end;

    n:=Blanks.Count;
    if n>FOption.NewBallCount then n:=FOption.NewBallCount;
    Randomize;
    for i:=1 to n do
    begin
      BallIndex:=Random(Blanks.Count);   //get a new ball from blanks
      j:=Pos(',',Blanks[BallIndex]);
      Row:=StrToInt(Copy(Blanks[BallIndex],1,j-1));
      Col:=StrToInt(Copy(Blanks[BallIndex],j+1,Length(Blanks[BallIndex])-j));
      Blanks.Delete(BallIndex);

      ColorIndex:=New3BallColor[i];
      if ColorIndex=-1 then ColorIndex:=1+Random(FOption.IconKind);   //ball color
      Bmp:=IconList[ColorIndex-1];
      WorkCanvas.BeginScene();
      try
        WorkCanvas.DrawBitmap(Bmp,RectF(0,0,Bmp.Width,Bmp.Height),
          RectF(Col*Gap+4,Row*Gap+4,Col*Gap+4+Bmp.Width,Row*Gap+4+Bmp.Height),
          100,false);
      finally
        WorkCanvas.EndScene;
      end;
      Bmp:=nil;
      BallList[Row,Col].Color:=ColorIndex;

      KillBall(Row,Col);
      PickOutBlank;   //pickout blank again
      if Blanks.Count=0 then
      begin
        CopyWorkCanvas;
        if Assigned(FOnGameOver) then FOnGameOver(self);
        exit;
      end;
    end;
  finally
    Blanks.Free;
  end;

  NewYuGao;

  DrawYuGao;
end;

在Windows下运行ok,但是在android下,总是在取出Bmp来用的部分这行崩溃:

Bmp:=IconList[ColorIndex-1];
错误为:Project flows.apk raised exception class Segmentation fault (11).

google查找,应该是"Automatic Reference Counting"引起的问题,在bmp变量前尝试加上[weak]或[Unsafe]都不行。

http://blog.marcocantu.com/blog/automatic_reference_counting_for_delphi.html

后来发现,IconList是classes.TList类型,在AUTOREFCOUNT下是禁止使用的,于是换成System.Generics.Defaults.TList<TBitmap>或System.Generics.Collections.TObjectList<TBitmap>,在android下测试ok。

原创文章 159 获赞 11 访问量 36万+

猜你喜欢

转载自blog.csdn.net/acrodelphi/article/details/50616888
今日推荐