高手帮我看看这段代码,为什么会出现访问内存违例?急急急
平台: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);
}
”
请问为什么会出现这个错误???