Strange code execution: execution begins in the middle of a while!

Hey guys! I have a thread. When the thread executes, an infinite while loop starts, which reads a member variable of the class.

The odd thing is that everything is fine to a certain point, where the while loop starts execution in the middle.

the code:

procedure MyThread.Execute;
var
  i: Integer;
begin
  NameThreadForDebugging('MyThread');

  while True do
  begin
    if (PrevLine1 <> CurLine1) and (PrevLine2 <> CurLine2) then
    begin

      {some code}

      PrevLine1 := CurLine1;   //<-- The place where execution starts...
      PrevLine2 := CurLine2;

      {some code}
    end;
    Sleep(500);
  end;

  self.Terminate;
  self.Destroy;
end;

I could place breakpoints in this procedure, but stops at

PrevLine1 := CurLine1;   //<-- The place where execution starts...

CurLine1 is a private member that can be changed using the property of the object. Everything works fine, but only once an erroneous behavior occurs.

Has anyone come across something similar or know what steps to take to fix this?

Thanks in advance!

+3
source share
7 answers

, ! , , Execute . , , .

, CPortLib (TComPort) . while, - ( if/else smth).

, ! , . !

+1

: Optimization=True. , , .

, "Debug" Delphi 2010 Optimization = True.

+3

, Delphi .

+2

, , , ? , ?

{some code}? if, false ?

+1

PrevLine1, PrevLine2, CurLine1 CurLine2? Execute, , , .

PrevLine1 := 1;
PrevLine2 := 1;
CurLine1 := 2;
CurLine2 := 2;

// The next line never has to execute on the first pass
// through the loop, as the test will always be true at
// this point.
if (PrevLine1 <> CurLine1) and (PrevLine2) <> (CurLine2) then
begin
  ..
end;

, , , CPU, NameThreadForDebugging. , .

+1

:

asm
 int 3
end;

, , , , - , NameThreadForDebugging.

-

+1

. , (, Formfeed). ctrl + C, ctrl + v , .

+1
source

Source: https://habr.com/ru/post/1794084/


All Articles