|
1. 동적할당과 RAD 툴과의 관계
VCL 컴포넌트는 동적할당만 가능하다고 알고 있습니다. 그 이유는 VCL이 오브젝트 파스칼로 쓰여있기 때문이구요. (정적할당을 지원하지 않으므로) 그런데, C#에서도 윈도프로그래밍을 할 때 컴포넌트는 동적할당만 가능합니다. 그래서 이러한 동적할당이 RAD툴의 일반적인 제약조건이 아닌가 하는 생각이 들었는데요. 상관관계가 있는지 궁금합니다.
2. contained component의 자동해제
container 컴포넌트가 contained 컴포넌트를 가질 때 container 컴포넌트가 해제되면 그 안의contained 컴포넌트도 자동으로 해제된다 쓰여있습니다. (C++ Builder 5 Developer's guide) 그런데 저자는 contained 컴포넌트도 명시적으로 해제해주는 것이 좋다라고 합니다. 예를 들어서,
TPanel* panel = new TPanel(this);
TLabel* label = new TLabel(this);
label->Parent = panel;
//explicit release
delete label;
delete panel;
"delete label" 이라는 코드는 필요없지만 명시적으로 해주라는 말 같은데요.
a) container 컴포넌트는 OWNER이어야 합니까? PARENT이어야 합니까? contained 컴포넌트의 메모리해제를 담당하는 것이 어느 것인지 알고 싶습니다.
b) 만일 프로그래머가 명시적으로 해제코드를 명시하면 빌더에서 자동으로 삽입하는 해제코드와 중복되지 않나요? 즉, 위예에서 panel의 소멸자에 delete label이라는 코드가 들어가 있지 않을까요? 그렇다면 이중 해제가 되지 않을까 해서요. 문제가 없을까요?
c) 이 부분에 대해 뉴스그룹에 질문을 올렸는데요. 어떤 분이 답변을 해 주셨는데 이해가 잘 안되네요. 설명 좀 부탁합니다.
[질문]
In the book, it is written that the contained component is also released
automatically when containing component is released.
1. The containing component is OWNER? or PARENT?
2. A programmer can release the contained component explicitly.
TPanel* panel = new TPanel(this);
TLabel* label = new TLabel(this);
label->Parent = panel;
//explicit release
delete label;
delete panel;
if the programmer does, i'm afraid that the code, generated automatically
by C++ Builder for releasing contained component, will conflict with the
code that programmer specified explicitly.
Is it true?
[답변]
I'm not certain you are correct. The constructor adds 'this' to a
list in the owner, it seems reasonable to assume that the destructor
removes 'this' from the same list, avoiding the possibility of double
deletion.
In case of doubt, the solution is obvious: whenever you create components
at runtime which you want to manage yourself, simply pass the Owner as 0.
foo()
{
std::auto_ptr<TPanel> panel(new TPanel(0));
// ...
}
I find this most useful for non-visual components that I use for database
access in multiple threads, but also use it for forms not created
automatically.
Arnold the Aardvark
|