Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Error in dynamic array TBytes: Delphi with Android


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Dec 11, 2017 11:18 AM Last Post By: Remy Lebeau (Te...
WILLIAM BENATTI

Posts: 5
Registered: 11/20/17
Error in dynamic array TBytes: Delphi with Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 8, 2017 5:35 PM
I'm trying to create new elements in a dynamic array with TBytes in Delphi. In Windows the routine below works fine, but when it runs on Android, shows up a error.

Is there a bug by using TBytes on Android?

var aaa: TBytes;
const
MY_CONST = 1;
begin
aaa := [MY_CONST]; // ERROR HERE
if aaa[0] = 0 then ShowMessage('ok');

Edited by: WILLIAM BENATTI on Dec 8, 2017 5:38 PM
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Error in dynamic array TBytes: Delphi with Android [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 8, 2017 7:03 PM   in response to: WILLIAM BENATTI in response to: WILLIAM BENATTI
WILLIAM BENATTI wrote:

I'm trying to create new elements in a dynamic array with TBytes in Delphi. In Windows the routine below works fine,
but when it runs on Android, shows up a error.

Is there a bug by using TBytes on Android?

var aaa: TBytes;
const
MY_CONST = 1;
begin
aaa := [MY_CONST]; // ERROR HERE
if aaa[0] = 0 then ShowMessage('ok');

Works fine for me on Android. What error do you receive?

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Error in dynamic array TBytes: Delphi with Android [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 8, 2017 8:21 PM   in response to: Dave Nottage in response to: Dave Nottage
Dave Nottage wrote:

Works fine for me on Android. What error do you receive?

See the discussion here:

https://stackoverflow.com/questions/47724135/

--
Remy Lebeau (TeamB)
WILLIAM BENATTI

Posts: 5
Registered: 11/20/17
Re: Error in dynamic array TBytes: Delphi with Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 9, 2017 5:50 PM   in response to: WILLIAM BENATTI in response to: WILLIAM BENATTI
I've found two issues while compiling my Android app (Android only. For Windows it works fine).

// ====================================================================
// First Problem: Compiler error for Android

*// My Function for test
function MyFunction(vData: TBytes; vLen: Integer = 0): Byte;
var i: Integer;
cks: Byte;
begin
cks := 0;

try
if vLen = 0 then vLen := Length(vData);

for i := 0 to vLen - 1 do
cks := cks + vData[i];
except on E: Exception do
end;

Result := cks;
end;

// Rotine error
var vData: TBytes;
cks: Byte;
begin
vData := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

// This TypeCast generates an error in memory management when run in Android (my Android version for test is 4.4)
cks := MyFunction(TBytes(@vData[2]), 3);
end;

// I solved problem this way:
var vFrAux: TBytes;
cks: Byte;
begin
vFrAux := Copy(vData, 2, 3);
cks := MyFunction(vFrAux, Length(vFrAux));


// ====================================================================
// Second Problem: Debugger error
var aaa: TBytes;
const
MY_CONST = 1;
begin
aaa := [MY_CONST]; // Error if breakpoint is set here.
if aaa[0] = 0 then ShowMessage('ok');

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Error in dynamic array TBytes: Delphi with Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 11, 2017 11:18 AM   in response to: WILLIAM BENATTI in response to: WILLIAM BENATTI
WILLIAM BENATTI wrote:

if vLen = 0 then vLen := Length(vData);

I would have opted to use -1 instead of 0, so that the caller can
specify 0 bytes be counted. But that is just personal preference.

// This TypeCast generates an error in memory management when run
in Android (my Android version for test is 4.4)

cks := MyFunction(TBytes(@vData[2]), 3);

That is an illegal type-cast in ALL platforms. You can't take the
memory address of an array element and type-cast it to a dynamic array
type like that. A dynamic array has metadata that would be missing in
such a cast. That is why you are getting errors at runtime.

To do what you are attempting, you MUST allocate a separate array and
copy the desired bytes into it, then pass the copy instead of the
original.

// I solved problem this way:
var vFrAux: TBytes;
cks: Byte;
begin
vFrAux := Copy(vData, 2, 3);
cks := MyFunction(vFrAux, Length(vFrAux));

Yes, exactly.

Otherwise, another solution is to simply not pass a dynamic array at
all, pass a pointer instead, eg:

function MyFunction(vData: PByte; vLen: Integer): Byte;
var
  i: Integer;
  cks: Byte;
begin
  cks := 0;
 
  try
    for i := 0 to vLen - 1 do
      cks := cks + vData[i];
  except
    on E: Exception do
  end;
 
  Result := cks;
end;
 
 
var
  vData: TBytes;
  cks: Byte;
begin
  vData := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
 
  cks := MyFunction(@vData[2], 3);
end;

// Second Problem: Debugger error
var
aaa: TBytes;
const
MY_CONST = 1;
begin
aaa := [MY_CONST]; // Error if breakpoint is set here.

That has nothing to do with the code itself. That is strictly an IDE
issue, and should be reported as a bug to Embarcadero.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02