问题是这样的:窗体上有个listview ,我用的是vsreport模式,然后建立8个列,然后设置listview的owndata=true,设置为virtual listview模式,在ondata事件里面增加了几个给listitem赋值的语句。然后建立一个thread,里面就一个语句 String p="fddfd";然后循环执行。程序运行的时候,线程中的语句报错,提示out of memory 我在线程里面创建任何vcl组件,比如TStringList Tlist,创建后,立刻删除。即使这样的代码,在运行的时候,也会出错。
C/C++ code//---------------------------------------------------------------------------#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include "Unit2.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TListView *lv1;
    TPanel *pnl1;
    TButton *btn1;
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall lv1Data(TObject *Sender, TListItem *Item);
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif// ---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TStringList *list;
TMyThread *thread;// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{list = new TStringList();
lv1->OwnerData=true;
// lv1->OwnerData=false;
thread = new TMyThread(false);
for (int i = 0; i < 200; i++)
{
String str = IntToStr(Random(99999)) + IntToStr(Random(99999)) + "额佛法我的看得人浮动框积分的放假了对抗赛";
list->Add(str);
}
lv1->Items->Count = list->Count;
}// ---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete list;
thread->Terminate();}// ---------------------------------------------------------------------------
void __fastcall TForm1::lv1Data(TObject *Sender, TListItem *Item)
{
if (Item->Index > list->Count)
return;Item->Caption = list->Strings[Item->Index];
Item->SubItems->Add(list->Strings[Item->Index]);
Item->SubItems->Add(list->Strings[Item->Index]);
Item->SubItems->Add(list->Strings[Item->Index]);
Item->SubItems->Add(list->Strings[Item->Index]);
Item->SubItems->Add(list->Strings[Item->Index]);Item->SubItems->Add(list->Strings[Item->Index]);
Item->SubItems->Add(list->Strings[Item->Index]);
}
// ---------------------------------------------------------------------------
C/C++ code
//---------------------------------------------------------------------------#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall TMyThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif[code=C/C++]
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "Unit2.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------__fastcall TMyThread::TMyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
//---- Place thread code here ----
while(!Terminated)
{
try
{
String p="ytytrytrytrytrythgmnytjythtss";
}
catch(Exception & e)
{
int i=0;
}
  }
}
//---------------------------------------------------------------------------
代码下载地址和视频演示在此下载,文件不大,只有200多K
http://pan.baidu.com/netdisk/singlepublic?fid=596138_634710353bcb和delphi差不多,我相信在delphi里也会有这个问题

解决方案 »

  1.   

    不管是否独立,有无交互,都不应该直接操作VCL
      

  2.   

    线程对 窗体上有个listview 有过操作吗?
    只是循环对字符串变量赋值?bcb这种 重复给字符串赋值 会引起内存泄漏吗?
      

  3.   

    你把线程关了,这个线程里就一个赋值操作,不create线程看看会不会报错呀。
      

  4.   

    while(!Terminated)
    {
    try
    {
    String p="ytytrytrytrytrythgmnytjythtss";
    }
    catch(Exception & e)
    {
    int i=0;
    }这里面是不是循环一次就得申请一次内存呀。一直为真就一直申请给P,造成内存溢出。
    String p="";
    int i=0;
    while(!Terminated)
    {
    try
    {
     p="ytytrytrytrytrythgmnytjythtss";
    }
    catch(Exception & e)
    {
     i=0;
    }这样行不?
      

  5.   

    测试了下,bcb6没问题,应该是2010的问题了