Watch, Follow, &
Connect with Us

Please visit our new home

Public Report
Report From: Delphi-BCB/Compiler/C++    [ Add a report in this area ]  
Report #:  3611   Status: Reported
VCL objects can be constructed on the stack...but not destructed
Project:  C++Builder Build #:  any
Version:    10.0 Submitted By:   Remy Lebeau (TeamB)
Report Type:  Minor failure / Design problem Date Reported:  2/24/2003 11:27:43 AM
Severity:    Infrequently encountered problem Last Updated: 8/17/2015 3:17:28 PM
Platform:    All platforms Internal Tracking #:   239931
Resolution: Retest (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (7 Total Ratings)
4.86 out of 5
Total Votes: None
The compiler allowes VCL objects to be constructed on the stack instead of the heap without error.  However, when constructed on the stack, the destructor is not called.

This has also been reported to QualityPortal as RSP-11640:
Steps to Reproduce:
Build the attached project.
Place a breakpoint in the destructor for Form2
Press button
Close application
Note that breakpoint is never hit.

Attached project was created using the following steps:

class TForm2
    __fastcall TForm2(TComponent *Owner);
    __fastcall ~TForm2();

__fastcall TForm2::TForm2(TComponent *Owner)
    : TForm(Owner)

__fastcall TForm2::~TForm2()

#include "Unit2.h"

void __fastcall TForm1::Button1Click(TObject *Sender)


void __fastcall TForm1::Button1Click(TObject *Sender)
    TForm2 *form = &TForm2(NULL);

In both cases, the TForm2 destructor is never entered.  If the form is auto-created, or created via 'new', then the destructor is called fine as normal.  This same behavior applies to other TObject-derived classes as well, not just TForm.
Explicitly call delete

chen yl at 9/16/2003 9:30:49 PM -
This VCL Object constructed just like on the stack ,but constructed on hte heap indeed,cmpilor call the constructor function as c++ language signification  and performed with Object Pascal  behavior .

So the code

TForm1 Form1 = new TForm1(NULL);


TForm1 Form1 = & TForm1(NULL);

will create same assemble code.
It is just distinguish of language signification.

Remy Lebeau (TeamB) at 8/17/2015 2:36:44 PM -
This is a compiler codegen bug.  The C++ compiler allows TObject-derived classes to be created on the stack but does not generate code to destruct them (no destructor is called).  I reported this problem 12 years ago, and can still reproduce it in XE8.

Once upon a time, trying to create a TObject-derived class on the stack would issue an error from the compiler:

"VCL style classes must be constructed using operator new."

That error is no longer issued in modern compiler versions.  But the codegen bug remains.

Server Response from: ETNACODE01