Problem with StringReplace in Delphi

0

I'm using FastMM to solve some problems in my TCC and I'm encountering this error in the StringReplace function:

  

The allocation number was: 180812

     

The block was previously freed by 0xAEC thread, and the stack trace   (return addresses) at the time was: 416091   [FastMM4.pas] [FastMM4] [FastMM4.DebugReallocMem] [8969] 4068E3   [System] [System. @ ReallocMem] 40B025 [System] [System. @ UStrSetLength]   40B0A6 [System] [System. @ UStrCat] 430078   [System.SysUtils] [System.SysUtils.StringReplace] 7C2D0B   [unFuncoes.pas] [unFuncoes] [unFuncoes.Results] [1682] 7C1D23   [unFuncoes.pas] [unFuncoes] [unFuncoes.GeraFitness] [1343] 7C19C3   [unFuncoes.pas] [unFuncoes] [unFuncoes.InsereIndividuos] [1251] 7BF40F   [unFuncoes.pas] [unFuncoes] [unFuncoes.AGCurvas] [216] 7C4F55   [unCadPredicao.pas] [unCadPredicao]   [unCadPredicao.TfrmCadPredicao.btnGerarClick] [107]   534C79 [Vcl.Controls] [Vcl.Controls.TControl.Click]

Here is the code for the Results function:

function Resultados(equacaoParam : ArrInteiro; vetX : array of Integer;
nRow : Integer) : resExt;
var
  EqOrganizada : String;
  k : Integer;
  Sup : resExt;
  I : Integer;
  Eval : TArtFormula;
  vars : array of string;
  vals : TCalcArray;
  z : Integer;
  b : Extended;
  begin
  Eval := TArtFormula.Create(nil);

  SetLength(Sup, nRow);
  //Gera o resultado do calculo de superficie,
  //de acordo com as equacoes existentes na populacao
    try
    try
      EqOrganizada := Organiza(equacaoParam);
      except
      EQOrganizada := '6(2)';
    end;

    EqOrganizada := StringReplace(EqOrganizada, '-10', 'z', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-1', 'a', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-2', 'b', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-3', 'c', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-4', 'd', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-5', 'f', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-6', 'h', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-7', 'i', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-8', 'j', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '-9', 'k', [rfReplaceAll]);

    EqOrganizada := StringReplace(EqOrganizada, '1', '+', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '2', '-', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '3', '/', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '4', '*', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '5', 'Exp', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '6', 'Log', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '7', '^', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '8', 'Sqrt', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, '9', '2^', [rfReplaceAll]);

    EqOrganizada := StringReplace(EqOrganizada, 'a', '1', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'b', '2', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'c', '3', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'd', '4', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'f', '5', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'h', '6', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'i', '7', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'j', '8', [rfReplaceAll]);
    EqOrganizada := StringReplace(EqOrganizada, 'k', '9', [rfReplaceAll]);

    for k := 0 to nRow-1 do
      begin
      z := vetX[k];
      SetLength(vars, 1);
      SetLength(vals, 1);
      vars[0] := 'z';
      b := z;
      setN(vals[0], b);
        try
        if ((AnsiContainsText(EqOrganizada, 'Log(z)') = true) and (z = 0)) or
         (AnsiContainsText(EqOrganizada, 'Sqrt(-') = true) or
         (AnsiContainsText(EqOrganizada, 'Log(-') = true) then
          begin
          Sup[k] := -1;
        end
        else
          begin
          ScanMemoryPoolForCorruptions;
          Sup[k] := Eval.ComputeStrN(EqOrganizada, 1, @vars, @vals);
        end;

        Except
        On E : Exception do
          begin
          Sup[k] := -1;
          //ShowMessage(E.Message);
        end;
      end;
    end;
    Except
    for I := 0 to nRow-1 do
      begin
      Sup[I] := (-1);
    end;
  end;
  eq := EqOrganizada;
  Result := Sup;
  Eval.Destroy;
end;

Is there a simpler way to do this? Or what about error? Thanks!

[EDIT]

Below is also the function code Organize:

function Organiza(vetEquacao : ArrInteiro) : string;
var
  sValor : String;
  len : Integer;
  I : Integer;
  Membro : Boolean;
  k : Integer;
  sfuncao : String;
  Termo1 : String;
  Termo2 : String;
  tpEquacao : ArrInteiro;
  j : Integer;
  tpVetEq : ArrInteiro;
  l : Integer;
  idx : Integer;
  begin
  len := Tamanho(vetEquacao);
  I := 1;
  l := 0;
  Membro := False;

  while I <= len do
    begin
    for k := 0 to Length(vetFuncoes)-1 do
      begin
      if vetEquacao[0][I-1] = vetFuncoes[k] then
        begin
        Membro := True;
        break;
      end;
    end;

    if (len = i) and (i <> 1) then
      begin
      break;
    end;

    if Membro then
      begin
      Membro := False;
      sfuncao := IntToStr(vetEquacao[0][I-1]);
      idx := FimEquacao(vetEquacao, i+1, 1);

      if (idx = 0) and ((Length(sValor)) > 150) then
        begin
        break;
      end;
      if (idx-(I+1)+1) >= 0 then
        begin
        SetLength(tpEquacao, 1, (idx-(I+1)+1));
      end
      else
        begin
        SetLength(tpEquacao, 1, 0);
      end;

      for k := I+1 to idx do
        begin
        tpEquacao[0][l] := vetEquacao[0][k-1];
        l := l + 1;
      end;
      l := 0;

      Termo1 := Organiza(tpEquacao);
      j := idx;

      for k := 0 to Length(vetFuncUnarias)-1 do
        begin
        if (StrToInt(sfuncao) = vetFuncUnarias[k]) then
          begin
          Membro := True;
          break;
        end;
      end;

      if Membro then
        begin
        Termo1 := Concat(sfuncao, Concat('(', Concat(Termo1, ')')));
        sValor := Concat(sValor, Termo1);
      end
      else
        begin
        idx := FimEquacao(vetEquacao, j+1, 1);
        if ((idx - (J+1))+1) >= 0 then
          begin
          SetLength(tpVetEq, 1, (idx - (J+1))+1);
        end
        else
          begin
          SetLength(tpVetEq, 1, 0);
        end;

        for k := j+1 to idx do
          begin
          tpVetEq[0][l] := vetEquacao[0][k-1];
          l := l + 1;
        end;
        l := 0;

        Termo2 := Concat(Organiza(tpVetEq), ')');
        Termo2 := Concat('(',Concat(Concat(Concat(Concat('(',Termo1)),')'), sfuncao),
        Concat(Concat('(', Termo2), ')'));
        sValor := Concat(sValor, Termo2);
      end;

      if idx = 0 then
        begin
        if j = 0 then
          begin
          i := len;
        end
        else
          begin
          i := j;
        end;
      end
      else
        begin
        i := idx;
      end;
    end
    else
      begin
      sValor := Concat(sValor, IntToStr(vetEquacao[0][i-1]));
    end;
    i := i + 1;
  end;
  Result := sValor;
end;

To improve understanding:

  • ArrInteiro is a type created, it is an "array of array of integer";
  • ResExt is a created type, it is an array of extended;
  • The program is not multi-threaded;
  • An example of the contents of the variable equationParam is: ((7, 9, 5, 4, 1, -10, 7, -5, -5, 7, 1, 9, 3, -8, -7,8,6,9, -9,0,0,0,0,0,0,0- 0, 0, 0, 0, 0, 0, 0, 0));
  • The content of vetX will always be (0, 1, 2, 3, 4);
  • E nRow is the size of VetX, ie 5;

In Organize we have the following constants:

asked by anonymous 16.02.2016 / 18:42

0 answers