Home > Back-end >  Invalid pointer operation, the problem of DELPHI
Invalid pointer operation, the problem of DELPHI

Time:09-24

Program runs without problems, but once click close error invalid pointer operation, added ShareMem in reference unit or an error, in Delphi directory can only find file: borlndmm. DLL, and relector open also can't see the inside what east east, ask ace to give directions, attached code:

The unit Unit1;

Interface

USES the
ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls ExtCtrls, Unit2,
OleCtrls, Registry, ComObj StrUtils;

Type
TForm1=class (TForm)
Timer1: TTimer;
Memo1: TMemo;
Timer2: TTimer;
Procedure Timer1Timer (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;
Var
Form1: TForm1;
I: integer;
HThread: THandle;

Implementation

{$R *. DFM}
The function ForLoop1 (p: Pointer) : Integer; Stdcall;
Var
I: integer;
The begin
For I:=1 to 500 do
The begin
Form1. Caption:=inttostr (I);
Form1. Memo1. Lines. The Add (RightStr (DateTimetostr (now ()), 8) + '_' + inttostr (I));
end;
Result:=0;
end;
Procedure TForm1. Timer1Timer (Sender: TObject);
Var
ID: DWORD;
The begin
Timer1. Enabled:=false;
HThread:=CreateThread (nil, 0, @ ForLoop1, nil, 0, ID);
end;
end.

CodePudding user response:

Thread resources have not been released
Simple solution is to set up compulsory exit sign, record increases the total number of threads in thread creation, records to reduce the total number of threads at the end of the thread, in the FORM of close events will be forced to exit the marked assignment, thread function have forced exit sign immediately end threads

This should be can not appear error

CodePudding user response:

Obviously, the main window is closed, the thread is not over,

CodePudding user response:

This is an invalid pointer manipulation,
Having canceled ShareMem unit, no longer exit error information,
As for your other questions, no code is speechless,

The test code is as follows:


 unit Unit1; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StrUtils, StdCtrls ExtCtrls;

Type
TForm1=class (TForm)
Memo1: TMemo;
Timer1: TTimer;
For: TButton;
Procedure Timer1Timer (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
I: integer;
HThread: THandle;

Implementation

{$R *. DFM}

The function ForLoop1: Integer; Stdcall;
Var
I: integer;
The begin
For I:=1 to 500 do
The begin
Form1. Caption:=inttostr (I);
Form1. Memo1. Lines. The Add (RightStr (DateTimetostr (now ()), 8) + '_' + inttostr (I));
end;
Result:=0;
end;

Procedure TForm1. Timer1Timer (Sender: TObject);
Var ID: DWORD;
The begin
Timer1. Enabled:=false;
HThread:=CreateThread (nil, 0, @ ForLoop1, nil, 0, ID);
end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
The close;
end;

End.

CodePudding user response:

Supplement: via detecting threads, has been closed, not thread did not shut down,

CodePudding user response:

Invalid pointer manipulation, additional access to the UI interface controls in the thread, need to increase the thread synchronization, and exit the program, you also need to send out to the thread end, both of you didn't do,

CodePudding user response:

How do you detect the thread has been closed?

Thread execution of poetry for loop, will soon be over,

When you close the window, if the thread is running, is in the for loop, there will be invalid pointer operation,,

In addition, the upstairs said is very reasonable!

CodePudding user response:

reference lyhoo163 reply: 3/f
this is invalid pointer manipulation,
Having canceled ShareMem unit, no longer exit error information,
As for your other questions, no code is speechless,

The test code is as follows:


 unit Unit1; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StrUtils, StdCtrls ExtCtrls;

Type
TForm1=class (TForm)
Memo1: TMemo;
Timer1: TTimer;
For: TButton;
Procedure Timer1Timer (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
I: integer;
HThread: THandle;

Implementation

{$R *. DFM}

The function ForLoop1: Integer; Stdcall;
Var
I: integer;
The begin
For I:=1 to 500 do
The begin
Form1. Caption:=inttostr (I);
Form1. Memo1. Lines. The Add (RightStr (DateTimetostr (now ()), 8) + '_' + inttostr (I));
end;
Result:=0;
end;

Procedure TForm1. Timer1Timer (Sender: TObject);
Var ID: DWORD;
The begin
Timer1. Enabled:=false;
HThread:=CreateThread (nil, 0, @ ForLoop1, nil, 0, ID);
end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
The close;
end;

End.

According to the code, add your other code, a try,

CodePudding user response:

1, form closed thread, not closed
2, the thread in the VCL, need special treatment

CodePudding user response:

Well, according to the thread used ForLoop1 the function, and the function of circulation is the use of the VCL control, error in understandable...
  • Related