Search code examples
delphiprocedureexecution

Procedure fires without being called


Is it possible, in delphi, that a procedure fires without being called?

I have two completly different procedure. First one is a click on popup menu. The second one is a function which i defined to split a string.

And i don't call my split method in my click of popup menu but it fires anyway and i can't find why. Debugger just says he can't read adress 00000001 but i don't even want him to read cause i don't call this procedure in any of my popup options. Does anyone have any idea of why it could fire by its own?

I can edit code if you want but idk it will be usefull as both procedure arent linked x)

CODE

procedure TBDDTool.pmDeleteColumnClick(Sender: TObject);
var
  i: integer;
  sListColNames : string;
begin
  fileModified := true;
  sListColNames := '';
  //Increment undo number
  Inc(undoNum);
  if undoNum = 11 then
    begin
      for i := 0 to Length(UndoArray) - 1 do
        begin
          if i < Length(UndoArray)-1 then
            UndoArray[i] := UndoArray[i+1];
        end;
    undoNum := UndoNum -1;
    end;
  //Add action to the array of undo actions
  undoArray[undoNum] := 'Deleted column:' + IntToStr(sgFilePreview.Col)
                       +'$'+aSourceData[0,sgFilePreview.Col] + '#deleted';
  pmUndo.Enabled := true;
  if (Pos('#primarykeypk', aSourceData[0, sgFilePreview.Col]) <> 0) then
    begin
      aSourceData[0,sgFilePreview.Col] := COPY(aSourceData[0,sgFilePreview.Col], 0, Pos('#primarykey', aSourceData[0, sgFilePreview.Col])-1);
      pmPrimaryKey.Enabled := true;
    end;

  if (Pos('#', aSourceData[0, sgFilePreview.Col]) <> 0) then
    aSourceData[0,sgFilePreview.Col] := COPY(aSourceData[0,sgFilePreview.Col], 0, Pos('#', aSourceData[0, sgFilePreview.Col])-1);

  for i := 0 to Length(aSourceData[0])-1 do
    begin
      if aSourceData[0,i] = sgFilePreview.Cells[sgFilePreview.Col, 0] then
      begin
        aSourceData[0,i] := aSourceData[0,i] + '#deleted';
        Break;
      end;
    end;

  //just set col width to 0 to hide it but we need the index
  sgFilePreview.ColWidths[sgFilePreview.Col] := 0;

end;

//Custom split method
function TBDDTool.Explode(const Separator, s: String;
  Limit: Integer): TStringDynArray;
var
  SepLen: Integer;
  F, P: PChar;
  ALen, Index: Integer;

begin

  SetLength(Result,0);
  //if the word passed is empty there's no need to continue
  if (S = '') or (Limit < 0) then Exit;
  if Separator = '' then
  begin
    SetLength(Result, 1);
    Result[0] := S;
    Exit;
  end;

  //Set to the length of the separator
  SepLen := Length(Separator);
  ALen := Limit;
  SetLength(Result, ALen);

  Index := 0;
  P := PChar(s);
  While P^ <> #0 do
  begin
    F := P;
    P := AnsiStrPos(P,PChar(Separator));
    if (P = nil) OR ((Limit > 0) AND (Index = Limit -1)) then  P := StrEnd(F);
    if Index >= ALen then
    begin
      Inc(ALen,5);
      SetLength(Result, ALen);
    end;
    SetString(Result[Index], F, P-F);
    INC(Index);
    if p^ <> #0 then Inc(P,SepLen);
  end;
  if index < ALen then SetLength(Result, Index);
  
end;

The explode functions is called when i click delet option (from a popup menu). But i don't call the explode function in my delete procedure. The break happens on while P^ <> #0 line


Solution

  • Is it possible, in delphi, that a procedure fires without being called?

    Generally speak, it is not possible. If code executes, something in the system made it execute.

    However, it is possible that you have somehow corrupted memory. That in turn may lead to you calling one function and the corruption leading to a different function being called.

    In order to debug this I suggest that you first of all inspect the call stack when the unexpected function begins executing. That should tell you how the execution reached that point. If that's not enough to explain things, cut your code down to the bare minimum that produces the problem. It's harder to find problems when there's lots of code. By cutting down to a minimum, you'll make it easier to see what has gone wrong.