救救我吧!请大侠们帮我看看这段代码哪儿错了,(100分)

  • 主题发起人 主题发起人 飞骐
  • 开始时间 开始时间

飞骐

Unregistered / Unconfirmed
GUEST, unregistred user!
头痛,这是我写的一段压缩文件的代码,其中的压缩函数在单线程中使用正常
unit CompressU;
interface
uses
Classes, SysUtils, ZLib;
//ZLib单元在Delphi的安装关盘中的Extra/Zlib目录中
type
TCompressThread = class(TThread)
private
{ Private declarations }
FFileName: string;
FCompressException: Exception;
procedure ShowException;
procedure CompressFile(var CompressingStream: TMemoryStream;
const CompressionLevel: TCompressionLevel);
protected
procedure Execute;
override;
public
constructor Create(const FileName: string);
end;

implementation
uses Forms;
{ TCompressThread }
procedure TCompressThread.CompressFile(
var CompressingStream: TMemoryStream;
const CompressionLevel: TCompressionLevel);
var
Count: integer;
DestStream: TMemoryStream;
SourceStream: TCompressionStream;
begin
Count:= CompressingStream.Size;
DestStream := TMemoryStream.Create;
SourceStream := TCompressionStream.Create(CompressionLevel, DestStream);
try
try
CompressingStream.SaveToStream(SourceStream);
SourceStream.Free;
CompressingStream.Clear;
CompressingStream.WriteBuffer(Count, SizeOf(Count));
CompressingStream.CopyFrom(DestStream, 0);
finally // wrap up
FreeAndNil(SourceStream);
FreeAndNil(DestStream);
end;
// try/finally
except
FCompressException:= ExceptObject as Exception;
Synchronize(ShowException);
end;
// try/except
end;

constructor TCompressThread.Create(const FileName: string);
begin
try
FFileName:= FileName;
inherited Create(False);
except
FCompressException := ExceptObject as Exception;
Synchronize(ShowException);
end;
// try/except
end;

procedure TCompressThread.Execute;
var
fs: TFileStream;
ms: TMemoryStream;
NewFileName: string;
begin
{ Place thread code here }
try
FreeOnTerminate := True;
NewFileName := ChangeFileExt(FFileName, '.dat');
fs:= TFileStream.Create(FFileName, fmOpenRead);
ms:= TMemoryStream.Create;
try
ms.LoadFromStream(fs);
CompressFile(ms, clDefault);
ms.SaveToFile(NewFileName);
finally // wrap up
FreeAndNil(fs);
FreeAndNil(ms);
end;
// try/finally
except
FCompressException := ExceptObject as Exception;
Synchronize(ShowException);
end;
// try/except
end;

procedure TCompressThread.ShowException;
begin
Application.ShowException(FCompressException);
end;

end.

这是主单元:
unit MainU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btnCompress: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
OpenDialog1: TOpenDialog;
procedure btnCompressClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FileName: string;
implementation
uses CompressU;
{$R *.dfm}
procedure TForm1.btnCompressClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
FileName:= OpenDialog1.FileName;
TCompressThread.Create(FileName);
end;
end;

end.
 
错误提示是什么?
 
在怎样运行的时候、在哪句代码上、出什么错误了?
 
他这不是提问,是考大家的水平呢,这段代码有错误,你能找出来吗?!
 
TCompressThread.Create(FileName);
改为
Var
XXX:TCompressThread;
XXX:=TCompressThread.Create(FileName);
试试。
 
to 小雨哥:试过了,不行的
 
to fatalexception:由于是多线程程序,因此程序的行为经常不一致,但出现得最多的
错误是access violation at address 00000000.read at address 00000000.
而且最终文件也没有被压缩。
 
跟踪一下看到哪里出错这各问题一般是内存不够或有冲突,
 
为什么不使用变量类似小雨那种,然后每次用完了释放?
不规范的书写方式容易给自己以后的调试等多方面带来麻烦。
拙见~
 
var
OpenDialog1:TOpenDialog;
Comp:TCompressThread;
begin
OpenDialog1 := TOpenDialog.Create(self);
OpenDialog1.DefaultExt := '.txt';
OpenDialog1.Filter := '*.txt,*.exe';
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
FileName:= OpenDialog1.FileName;
comp := TCompressThread.Create(FileName);
comp.Free;
end;

end;

此代码,delphi7下,调试通过。
硬盘刚修好,没来得及装其他的版本,自己试试吧。
 
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1354567
关注一下吧没有人提起来呀!用MODEM打电话利用PC机的耳机和麦克风,自己编程如何实现?
(我知道有现成的软件可以用,但是我想自己实现这个上东西)
QQ:65466700
MAIL: along@b2sun.com
TEL :13802785865
http://b2sun.com
请各位大侠多多指教!
 
我对Delphi不是很熟,现在也无法调试,但我看了你的代码,我有以下疑问:
依据你的错误处理过程,你希望的错误处理是当压缩过程发生错误时,显示错误信息,
接着退出压缩线程。这完全可以同传递错误信息字符串实现,而不用传递错误对象。
如果一定要传递错误对象,通过你的方式,我怀疑会发生问题。我想ExceptObj对象只
是在Except...end过程块中有效,通过FCompressException变量传送到ShowException方
法,已变为无效对象。
 
测试通过的代码,自己去改吧:
 
去掉 SourceStream.Free;
这句代码就可以了
procedure TCompressThread.CompressFile(
var CompressingStream: TMemoryStream;
const CompressionLevel: TCompressionLevel);
var
Count: integer;
DestStream: TMemoryStream;
SourceStream: TCompressionStream;
begin
Count:= CompressingStream.Size;
DestStream := TMemoryStream.Create;
SourceStream := TCompressionStream.Create(CompressionLevel, DestStream);
try
try
CompressingStream.SaveToStream(SourceStream);
//*****************************
//SourceStream.Free;
//*****************************
CompressingStream.Clear;
CompressingStream.WriteBuffer(Count, SizeOf(Count));
CompressingStream.CopyFrom(DestStream, 0);
finally // wrap up
FreeAndNil(SourceStream);
FreeAndNil(DestStream);
end;
// try/finally
except
FCompressException:= ExceptObject as Exception;
Synchronize(ShowException);
end;
// try/except
end;
 
接受答案了.
 
后退
顶部