高手帮我看看这段代码,为什么会出现访问内存违例?急急急

aatny1 2008-10-17 05:09:08
平台:C++ builder6.

头文件:test.h

//---------------------------------------------------------------------------

#ifndef testH
#define testH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

//#include "RzPanel.hpp"
#include <vector.h>
//---------------------------------------------------------------------------
class MyPanel;
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
vector <MyPanel*> ParaPanels;
};

class MyPanel : public TPanel
{
private:

public:
__fastcall MyPanel(TComponent* Owner);
vector <int> number;
vector <float> value;

__fastcall ~MyPanel();
};
__fastcall MyPanel::MyPanel(TComponent* Owner) : TPanel(Owner{}
__fastcall MyPanel::~MyPanel()
{
if(value.size() > 0)
{
number.clear();
value.clear();
ShowMessage("delete panel");
}
}
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
////////////////////////////////////////////////////////////////////////////////
CPP文件:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "test.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (int i = 0;i < 10;i++)
{
MyPanel* panel = new MyPanel(this) ;
panel->Parent = this;
panel->Width = 38; //
panel->BorderWidth = 0;
panel->Top = 100;
panel->Left = 10 + i * 40;
panel->Height = 20;

this->ParaPanels.push_back(panel);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for(int i = 0;i < this->ParaPanels.size();i++)
{
for (int j = 0;j < 1000;j++)
{
this->ParaPanels[i]->number.push_back(j + 1);
this->ParaPanels[i]->value.push_back(j * 1.2f);
}
}
}
//---------------------------------------------------------------------------

编译运行后,先按button1添加panels,然后按button2往vector里添加数据。当关闭程序时,提示:“Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00000000.
Read of address 00000000'. Process stopped. Use Step or Run to continue.”请问为什么会出现这个错误。据我所知,错误的起源在于这段代码
“for (int j = 0;j < 1000;j++)
{
this->ParaPanels[i]->number.push_back(j + 1);
this->ParaPanels[i]->value.push_back(j * 1.2f);
}

请问为什么会出现这个错误???
...全文
218 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2008-10-18
  • 打赏
  • 举报
回复
额主动检查...
chenyi3315 2008-10-18
  • 打赏
  • 举报
回复
up。。。
backway 2008-10-18
  • 打赏
  • 举报
回复
mark
aatny1 2008-10-17
  • 打赏
  • 举报
回复
找到原因了,是关闭程序时,没有手动delete MyPanel*.
  • 打赏
  • 举报
回复
ShowMessage里面用到了其他的全局变量,有可能是这个原因造成的.
所以会有访问内存的问题.
  • 打赏
  • 举报
回复
ShowMessage("delete panel"); 

改为
MessageBox(0,"delete panel",0,0);
boys2002 2008-10-17
  • 打赏
  • 举报
回复
不敢乱说



先帮顶下~~~~~~~~~~~~~~~~·



先关注一下
帅得不敢出门 2008-10-17
  • 打赏
  • 举报
回复
for (int i = 0;i < 10;i++)
{
MyPanel* panel = new MyPanel(this) ;
panel->Parent = this;
panel->Width = 38; //
panel->BorderWidth = 0;
panel->Top = 100;
panel->Left = 10 + i * 40;
panel->Height = 20;

this->ParaPanels.push_back(panel);
}

内存释放清楚了没有

帅得不敢出门 2008-10-17
  • 打赏
  • 举报
回复
输出this->ParaPanels.size();看看
baihacker 2008-10-17
  • 打赏
  • 举报
回复
看上去这些都是正确初始化了的.你跟一下,到j循环前,看看
this->ParaPanels[i]
是不是NULL

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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