Recent

Author Topic: [SOLVED] Put function result as variable is always AV, Why?  (Read 3506 times)

Человек_Борща

  • New Member
  • *
  • Posts: 33
    • My little IT world.
[SOLVED] Put function result as variable is always AV, Why?
« on: January 08, 2015, 02:05:25 pm »
Greetings!

I put initialized string variable as result or as out param to some function, that returns me HEX-string. But after some magic, out variable doesn't exists anymore.

See code and comments.

This problem makes me a bit crazy today.  >:(

Code: [Select]

procedure MAINROUTIME()
const
  INT_ONE = 1; //even or INT_ONE:Integer = 1; gives me the same result
var
 s1:string;
begin
  s1 := ''; //Init s1
   GetTigerHash(IntToStr(INT_ONE),s1); //Step 1. Send '1'
end;

function GetTigerHash(sData: string; out Str: string): boolean; overload;
var
  hash: TDCP_tiger;
  TigerBuff: array[0..2] of byte;
begin
  Result := False;
  Str := ''; //Step 2,reinit out param, all is ok. Str are accessible
  hash := TDCP_tiger.Create(nil);
  try
    hash.Init;
    hash.UpdateStr(sData);
    hash.Final(TigerBuff); //Here I have array result bytes {29,87,49}
    Str := BinToStr(TigerBuff); //Step 3 into BinToStr, see it below
   // STEP 5 - Str isn't accessible anymore. I'm going away.... with access violation
    Result := True;
  finally
    FreeAndNil(hash);
  end;
end; 

function BinToStr(const bin: array of byte): string;
var
  i: integer;
  s: string;
begin
  Result := '';
  s := '';
  for i := low(bin) to high(bin) do
    s := s + IntToHex(bin[i], 2);
  Result := s; //Step 4, Here, I see the s variable contains string "1D5731"
end;



Why that happens!? This also happens when I use a function result instead of out param.
Why after hash.final() out str param is not accessible anymore?

It makes me crazy...

I use official Lazarus releasefrom web-site, is a 1.2.6 Lazarus.
« Last Edit: January 08, 2015, 03:34:47 pm by Человек_Борща »
With best regards, Alexandr.
_
OS: Windows 8.1 x64 / Ubuntu 14.02 x64
IDE: CodeTyphon 5.4 (win32-win64/win64) / FPC 3.1.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Put function result as variable is always AV, Why?
« Reply #1 on: January 08, 2015, 02:32:49 pm »
I cannot reproduce with this simple program:
Code: [Select]
{$mode objfpc}

procedure p(out o: string);
begin
  o := '';
  o := 'test';
end;

var
  s: string;
begin
  p(s);
  WriteLn(s);
end.
From the latest source, Final method expects array[0..2] of Int64, not array[0..2] of Byte. You clearly have broken the stack.

Человек_Борща

  • New Member
  • *
  • Posts: 33
    • My little IT world.
Re: Put function result as variable is always AV, Why?
« Reply #2 on: January 08, 2015, 03:34:30 pm »
 :o Thanks. Now it works fine.
With best regards, Alexandr.
_
OS: Windows 8.1 x64 / Ubuntu 14.02 x64
IDE: CodeTyphon 5.4 (win32-win64/win64) / FPC 3.1.1

 

TinyPortal © 2005-2018