폼 하나에다 버튼하나가 있고 그 버튼을 클릭하면 달력컴포넌트(TMonthCalendar)가 뜨게 합니다. 거기서 날짜정보를 받고 이 컴포넌트를 닫으려고 다음과 같이 해주니깐 access violation error가 납니다.
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
calendar = new TMonthCalendar(this);
calendar->Parent = Form1;
calendar->Left = 68;
calendar->Top = 240;
calendar->Width = 60;
calendar->Height = 70;
calendar->OnClick = calendarEvent;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::calendarEvent(TObject *Sender)
{
TDateTime pdate;
AnsiString str;
pdate = calendar->Date;
str = pdate.DateString();
ShowMessage(str.SubString(1,4));
ShowMessage(str.SubString(6,2));
ShowMessage(str.SubString(9,2));
delete calendar; // <-- 이 부분 실행 후 에러 발생.
}
저도 new 한 함수와 delete한 함수가 서로 다른데서 찜찜한 감이 오긴 왔습니다만...
보통 컴포넌트를 동적으로 불러내고 메모리에서 삭제하려면 이런 식으로 하면 안되는 건가요?
아예 그냥 프로그램에서 알아서 힙 메모리에서 해제하도록 delete 안하는게 나을까요?
|