十万火急,为什么以下的程序会引发非法操作?

bluesnail 2001-05-13 08:40:00
每当连续运行几分钟后,就会引起异常“out of memory while expanding memory stream!"
在程序中,我每接收完一次stream后都有将它清空的,应该不会out of memory的哇。
请各位帮忙看看是不是我的代码有什么问题?

其中TForm1::ScreenCap用于截取屏幕的,而TMyThread::Excute是用来传送数据的。
以下是unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <NMUDP.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread //User define by cjq
{
public:
__fastcall TMyThread(void);
private:
void __fastcall Execute(void);
};
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label0;
TBevel *Bevel0;
TStaticText *StaticText0;
TButton *Button1;
TNMUDP *NMUDP1;
TImage *Image1;
TComboBox *ComboBox1;
TTimer *Timer1;
TSpeedButton *SpeedButton1;
TLabel *Label1;
TLabel *Label3;
TSpeedButton *SpeedButton2;
TLabel *Label2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall SpeedButton1Click(TObject *Sender);
void __fastcall NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall ScreenCap(int LeftPos,int TopPos,int RightPos,int BottomPos);
TMemoryStream *BmpStream;
TMemoryStream *RsltStream;
TMemoryStream *TmpStream;
TMyThread *MyThread;
bool Send;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

以下是unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Send = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Send = !false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ScreenCap(int LeftPos,int TopPos,int RightPos,int BottomPos)
{
int RectWidth,RectHeight;
HDC SourceDC,DestDC,Bhandle;
Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
RectWidth = RightPos-LeftPos;
RectHeight = BottomPos-TopPos;
// SourceDC = CreateDC("DISPLAY","","",NULL);
SourceDC = CreateDC("DISPLAY",NULL,NULL,NULL);
DestDC = CreateCompatibleDC(SourceDC);
Bhandle = CreateCompatibleBitmap(SourceDC,RectWidth,RectHeight);
SelectObject(DestDC,Bhandle);
BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,LeftPos,TopPos,SRCCOPY);
Bitmap->Handle = Bhandle;
BmpStream->Clear();//清空流
Bitmap->SaveToStream(BmpStream);

// Canvas->Draw(0,0,Bitmap);
// Bitmap->SaveToFile("Temp.bmp");
delete Bitmap;
DeleteDC(DestDC);
ReleaseDC(Bhandle,SourceDC);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
BmpStream = new TMemoryStream();
// BmpStream->Size = 1024*768*3;
RsltStream = new TMemoryStream();
RsltStream->Size = 1024*768*3;
TmpStream = new TMemoryStream();
MyThread = new TMyThread; //Start the background thread
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if(SpeedButton1->Down)
{
CanClose = false;
return;
}
Timer1->Enabled = false;
MyThread->Terminate(); //Stop the background thread
delete BmpStream;
delete RsltStream;
delete TmpStream;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Button1Click(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
Button1->Enabled = !SpeedButton1->Down;
ComboBox1->Enabled = !SpeedButton1->Down;
Timer1->Enabled = SpeedButton1->Down;
}
//---------------------------------------------------------------------------
__fastcall TMyThread::TMyThread(void) : TThread(TRUE)
{
Priority = tpIdle;
Resume(); //Resume the thread
}
//----------------------------------------------------------------------------
void __fastcall TMyThread::Execute(void)
{
while(1)
{
if(Form1->Send)
{
if(Form1->SpeedButton2->Down)
Form1->ScreenCap(0,0,350,200);
else
Form1->ScreenCap(0,0,Screen->Width,Screen->Height);

const int ThreshHold = 2048;
Form1->BmpStream->Position = 0;

char *Buffer = new char[ThreshHold];
int BuffSize = Form1->BmpStream->Size;
int Buffers = BuffSize/ThreshHold;
Form1->NMUDP1->RemoteHost = Form1->ComboBox1->Text;
for(int i=0; i <= Buffers; i++)
{ Sleep(1);
if(BuffSize > 2048)
{
Form1->BmpStream->Read(Buffer, ThreshHold);
Form1->NMUDP1->SendBuffer(Buffer, ThreshHold, ThreshHold);
} else
{
if(BuffSize<=0) break;
Form1->BmpStream->Read(Buffer, BuffSize);
Form1->NMUDP1->SendBuffer(Buffer, BuffSize, BuffSize);
}
BuffSize -= ThreshHold;
}
Form1->NMUDP1->SendBuffer("E", 1, 1);
delete Buffer;
}
Form1->Send = false;
}
}
//----------------------------------------------------------------------------


void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
TmpStream->Clear();
if(NumberBytes != 1) //数据已读完
{
NMUDP1->ReadStream(TmpStream);
RsltStream->CopyFrom(TmpStream,NumberBytes);
}
if(NumberBytes == 1) //数据已读完
{
RsltStream->Position = 0;
try{
Image1->Picture->Bitmap->LoadFromStream(RsltStream);
}
catch ( ... )
{
}
RsltStream->Clear();
}
}
//------------------------------------------------------------------------



...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesnail 2001-05-15
  • 打赏
  • 举报
回复
我曾经试过加上
BmpStream->Clear();
但是问题还是依然存在,是不是我请空内存流的方法不对呢?
xiaozi 2001-05-15
  • 打赏
  • 举报
回复
你的 Email 呢? 找到后我 mail 给你!
holyfire 2001-05-15
  • 打赏
  • 举报
回复
试试将BmpStream->Position = 0;
holyfire 2001-05-14
  • 打赏
  • 举报
回复
Sleep(1);
if(BuffSize > 2048)
{
Form1->BmpStream->Read(Buffer, ThreshHold);
Form1->NMUDP1->SendBuffer(Buffer, ThreshHold, ThreshHold);
} else
{
if(BuffSize<=0) break;
Form1->BmpStream->Read(Buffer, BuffSize);
Form1->NMUDP1->SendBuffer(Buffer, BuffSize, BuffSize);
}
BuffSize -= ThreshHold;

BmpStream
没见到将其清空或者复位的啊

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧