Problem with Stringreplace in Delphi

Asked

Viewed 420 times

0

I am using Fastmm to solve some problems in my TCC and am encountering this error in Stringreplace function:

The allocation number was: 180812

The block was Previously Freed by thread 0xAEC, 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.Inseindividuos][1251] 7BF40F [unFuncoes.pas][unFuncoes][unFuncoes.Agcurvas][216] 7C4F55 [unCadPredicao.pas][unCadPredicao] [unCadPredicao.TfrmCadPredicao.btnGerarClick][107] 534C79 [Vcl.Controls][Vcl.Controls.Tcontrol.Click]

Follow the code of 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 any simpler way to do this? Or what of mistake? Thank you!

[EDIT]

Below is also the code of the function 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 type created, it is an array of Extended;
  • The program is not multi-threaded;
  • An example of the contents of the equation variable 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, 0, 0, 0));
  • The contents of vetX will always be (0, 1, 2, 3, 4);
  • And nRow is the size of Vetx, ie 5;

In Organiza we have the following constants:

  • vetFuncoes : array [0.. 8] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9);

  • vetFuncUnarias : array [0.. 5] of Integer = (1, 2, 5, 6, 8, 9);

  • 1

    Welcome to Sopt. To highlight codes, select the entire block and click {}. The apostrophe is used only for code of a line or shorter.

  • Thanks for the help!

  • At what point are you giving error, already debugged to see this? The application is multi-thread?

  • Welcome to Sopt. You need to make a full minimum application that demonstrates the error.

  • @DH. I tried to debug, but the program goes through this part of the code hundreds of times, it is hard to find the error via debug.

  • This is why you need to create a complete minimum application that demonstrates the error. See: https://answall.com/help/mcve

  • I was able to solve the problem. It only replaces Stringreplace with Eqorganized.Replace. I also set the arrays as nil at the end of Function.

  • 1

    Jordan, the site is not a forum, it’s questions and answers, so you can post the solution yourself as an answer, right below, and mark as accepted. Neither need to put [solved], doing what I told you, already shows that it was solved :)

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.