多线程操作IntToStr()函数报错,如何解决(100分)

  • 主题发起人 huwei1118
  • 开始时间
H

huwei1118

Unregistered / Unconfirmed
GUEST, unregistred user!
一个时间控件,一个线程,两个都用到了inttostr()函数 ,同时运作时执行到inttostr时就会报错:非法的指针操作 不光是inttostr() 我发现只要同时操作字符串类也一样会出错,一个用inttostr() 另一个用format() 也一样出错。请高手帮忙看看。
 
应该与函数无关,很有可能是函数的参数问题,你没有做好作为参数的全局变量的同步
 
把代码帖上来看一下啦,否则只是你说的不可以发生的啊~
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function CheckLcdStr(LCDStr:String;ch:Integer):String;
var
I:Integer;
CallID : array[0..47] of char;
TempLCDStr : String;
begin
StrCopy(CallID,PChar(Copy(LCDStr,1,46)));
Result:='';
TempLCDStr:=LowerCase(LCDStr);
if pos('from',TempLCDStr)>0 then
begin
for I:=0 to 47do
begin
if CallID in ['0'..'9'] then
begin
Result:=Result+ String(CallID);
end;

if (CallID in [' ']) and (Result<>'') then
break;
end;

if Result='' then
Exit;
end;

end;

function ThreadFunc3(p:pointer):LongInt;stdcall;
var
a:String;
l:integer;
begin
for l:=0 to 10000000do
begin
a:=inttostr(l);
end;
end;


function ThreadFunc5(p:pointer):LongInt;stdcall;
var
b:String;
l:integer;
begin
for l:=0 to 10000000do
begin
b:=inttostr(l);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
hThread:THandle;
ThreadID:DWORD;
i:integer;
begin

hthread:=CreateThread(nil,
0,
@THreadFunc3,
nil,
0,
ThreadID);
if hthread=0 then
MessageBox(Handle,'NO Thread',nil,MB_OK)
else
closehandle(hthread);
hthread:=CreateThread(nil,
0,
@THreadFunc5,
nil,
0,
ThreadID);
if hthread=0 then
MessageBox(Handle,'NO Thread',nil,MB_OK)
else
closehandle(hthread);

end;

end.
 
在执行button2 就出错了。
 
和函数参数无关 ,只是两个线程同时操作字符串类就出问题,不论是什么字符串操作函数,也行函数的参数没有关系。
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function CheckLcdStr(LCDStr:String;ch:Integer):String;
var
I:Integer;
CallID : array[0..47] of char;
TempLCDStr : String;
begin
StrCopy(CallID,PChar(Copy(LCDStr,1,46)));
Result:='';
TempLCDStr:=LowerCase(LCDStr);
if pos('from',TempLCDStr)>0 then
begin
for I:=0 to 47do
begin
if CallID in ['0'..'9'] then
begin
Result:=Result+ String(CallID);
end;

if (CallID in [' ']) and (Result<>'') then
break;
end;

if Result='' then
Exit;
end;

end;

function ThreadFunc3(p:pointer):LongInt;stdcall;
var
a:String;
l:integer;
begin
for l:=0 to 10000000do
begin
a:=inttostr(l);
sleep(1);//增加--------------------------
end;
end;


function ThreadFunc5(p:pointer):LongInt;stdcall;
var
b:String;
l:integer;
begin
for l:=0 to 10000000do
begin
b:=inttostr(l);
sleep(1);//增加--------------------------
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
hThread:THandle;
ThreadID:DWORD;
i:integer;
begin

hthread:=CreateThread(nil,
0,
@THreadFunc3,
nil,
0,
ThreadID);
if hthread=0 then
MessageBox(Handle,'NO Thread',nil,MB_OK)
else
closehandle(hthread);
hthread:=CreateThread(nil,
0,
@THreadFunc5,
nil,
0,
ThreadID);
if hthread=0 then
MessageBox(Handle,'NO Thread',nil,MB_OK)
else
closehandle(hthread);

end;

end.
 
呵呵,我加的sleep(0)也没问题,还在查找出错的原因,感觉与String的内存管理有关
 
关于String 的问题,以前有过讨论
http://www.delphibbs.com/keylife/iblog_show.asp?xid=30419
 
function IntToStr(Value: Integer): string;
// FmtStr(Result, '%d', [Value]);
asm
PUSH ESI
MOV ESI, ESP
SUB ESP, 16
XOR ECX, ECX // base: 0 for signed decimal
PUSH EDX // result ptr
XOR EDX, EDX // zero filled field width: 0 for no leading zeros
CALL CvtInt
MOV EDX, ESI
POP EAX // result ptr
CALL System.@LStrFromPCharLen
ADD ESP, 16
POP ESI
end;
看看函数再说吧
 
IsMultiThread := TRUE 问题解决
 
顶部