I have a cross platform C++ application build with C++ Builder 10.1 Berlin and have a problem understanding the lifetime handling of objects, in this case strings, wich are declared outside the class.
I have created a new forms application and added some code. The cpp file looks like this:
#include
#pragma hdrstop
#include "FmrMain.h"
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
const String Hello = "Hello";
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ShowMessage(Hello);
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ShowMessage(Hello);
}
I compile this with the CLANG enhanced C++11 compiler bcc32c, run the application and close the form again. When TForm1::FormDestroy is called Hello is allready destroyed. When I compile the code for win32 with the classic compiler bcc32 the string is destroyed after FormDestroy.
Can someone explain this or provide some information about the topics I have to look for? Why is the CLANG based compiler behaving different here?
Edit
It's easier to debug when I use a self defined class instead of a string.
class Foo {
public:
Foo(){};
~Foo(){}
};
Foo A;
//--------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
}
The creation and destruction oder is like this. I have added the call stacks.
bcc32c (CLANG C++11 compiler)
create Foo
:004052C0 Foo(this=:00400000)
:00405070 __cxx_global_var_init3()
:004052A3 _GLOBAL__I_a()
:00405ab7 ; ~Foo
:321fa2b7 ; C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\CC32C240MT.DLL
:321fa6ff CC32C240MT.__wstartup + 0xbb
create Form1
:004052EC TForm1(this=:00402422, __ctor_flag='\0')
:0085c139 fmx240.#Fmx#Forms#TApplication#CreateForm$qqrxp17System#TMetaClasspv + 0x5d
:0085c349 fmx240.#Fmx#Forms#TApplication#RealCreateForms$qqrv + 0x81
destroy Foo
:004052D0 ~Foo(this=:0040B7DC)
:0040509E __dtor_A()
:321f6246 CC32C240MT.___call_atexit_procs + 0x52
:321f671c CC32C240MT.___exit + 0x20
destroy Form1
:00405868 ~TForm1(this=:5016E698)
bcc32 (Classic borland compiler)
create Foo
:00404950 Foo::Foo(this=:00409B74)
:004048A0 STCON0()
:00405727 ; IRoot
:322190f1 ; C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\CC32240MT.DLL > :322193b5 CC32240MT.__wstartup + 0xa5
create Form1
:00404994 TForm1::TForm1(this=:02F2AE20, Owner=:02F39620)
:0095c139 fmx240.#Fmx#Forms#TApplication#CreateForm$qqrxp17System#TMetaClasspv +
0x5d
:0095c349 fmx240.#Fmx#Forms#TApplication#RealCreateForms$qqrv + 0x81
destroy Form1
:00404ABC TForm1::~TForm1(this=:02F2AE20)
destroy Foo
:00404978 Foo::~Foo(this=:00409B74)
:0040493F STDES0()
:0040573f ;IRoot>
:3221910f ; C:\Program Files(x86)\Embarcadero\Studio\18.0\bin\CC32240MT.DLL
:3221915b ; C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\CC32240MT.DLL > :3221944a ; C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\CC32240MT.DLL
Auto-created TForm objects are owned by the global TApplication object. That object is destroyed (thus destroying its owned Forms) after the application's main()/wmain()/WinMain() entry point function has exited. Globals are destroyed during application cleanup.
The lifetime of your global String is not guaranteed to outlive the lifetime of the global TApplication object, in either compiler. You are relying on undefined behavior based on the cleanup order of globals across different units. And worse, you are relying on the cleanup order across different frameworks! Your String is in your own C++ code, but the TApplication object is in the Delphi-based RTL library.
If your String needs to remain alive for the lifetime of the TForm that uses it, you should declare it as a static member of that class instead:
FmrMain.h:
//---------------------------------------------------------------------------
#ifndef FmrMainH
#define FmrMainH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
static const String Hello;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
FmrMain.cpp:
//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop
#include "FmrMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
const String TForm1::Hello = "Hello";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ShowMessage(Hello);
}
void __fastcall TForm1::~TForm1()
{
ShowMessage(Hello);
}
//---------------------------------------------------------------------------
Alternatively, use a wchar_t* instead of a String, then you don't run into any cleanup issues:
//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop
#include "FmrMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
static const wchar_t* Hello = L"Hello";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ShowMessage(Hello);
}
void __fastcall TForm1::~TForm1()
{
ShowMessage(Hello);
}
//---------------------------------------------------------------------------
Related
Getting started with Embarcadero XE-5, the object model has me confused.
My project involves the Canvas right from the start, so my hello world
is to draw a line or two. Set up an SDI project, and added a fastcall
directly out of the C++ builder help, but can't get it to compile. Form1
is used in all the examples, but my efforts to instantiate it aren't
working. I've tried to declare Form1 in various ways, no success.
Can anyone point out my error, please?
// ----------------------------------------------------
#include <vcl.h>
#pragma hdrstop>
#include <tchar.h>
//-----------------------------------------------------
USEFORM("SDIMAIN.CPP", SDIAppForm);
USEFORM("ABOUT.CPP", AboutBox);
//-----------------------------------------------------
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
Application->Initialize();
Application->CreateForm(__classid(TSDIAppForm), &SDIAppForm);
// ** Following line gives error: Form1 undefined. **
Application->CreateForm(__classid(TCanvas), &Form1);
Application->CreateForm(__classid(TAboutBox), &AboutBox);
Application->Run();
return 0;
}
//------------------------------------------------------
/* SDIMAIN - copied from the help screens */
void __fastcall TForm1::FormPaint(TObject *Sender)
{
Canvas->MoveTo(0,0);
Canvas->LineTo(ClientWidth, ClientHeight);
Canvas->MoveTo(0, ClientHeight);
Canvas->LineTo(ClientWidth, 0);
}
You don't use TApplication::CreateForm() to create TCanvas objects. Change __classid(TCanvas) to __classid(TForm1) instead:
// ----------------------------------------------------
#include <vcl.h>
#pragma hdrstop>
#include <tchar.h>
//-----------------------------------------------------
USEFORM("SDIMAIN.CPP", SDIAppForm);
USEFORM("Unit1.cpp", Form1);
USEFORM("ABOUT.CPP", AboutBox);
//-----------------------------------------------------
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
Application->Initialize();
Application->CreateForm(__classid(TSDIAppForm), &SDIAppForm);
Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TAboutBox), &AboutBox);
Application->Run();
return 0;
}
//------------------------------------------------------
Of course, this requires you to have a TForm1 class to begin with:
File > New > VCL Form
I'm trying to create a simple custom control using Borland C++ Builder 6. In this case, I am trying to create a TPageControl with a single TTabSheet on it. I am having trouble figuring out the proper place to initialize these child controls. At the moment I am initializing everything in the constructor. Everything compiles fine, but when I attempt to place the control onto a form, the Borland IDE gives me an error "Control '' has no parent window" or something very similar. I've found out that the line that is causing this specifically is the setting of the TTabSheet's PageControl property.
The code for my control is below:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TestControl.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TTestControl *)
{
new TTestControl(NULL);
}
//---------------------------------------------------------------------------
__fastcall TTestControl::TTestControl(TComponent* Owner)
: TCustomControl(Owner)
{
pageControl = new TPageControl(this);
pageControl->Parent = this;
tabSheet = new TTabSheet(pageControl);
tabSheet->Parent = pageControl;
tabSheet->Caption = "Page 1";
tabSheet->PageControl = pageControl;
}
//---------------------------------------------------------------------------
__fastcall TTestControl::~TTestControl()
{
pageControl->Free();
}
//---------------------------------------------------------------------------
namespace Testcontrol
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TTestControl)};
RegisterComponents("Test", classes, 0);
}
}
//---------------------------------------------------------------------------
Any assistance would be much appreciated--I'm finding that due to the age of this particular technology I'm not having much luck finding resources on this.
I build the code successfully but it does not debug and comes up with this warning in a wizard miscellaneous code.
"Warning: Destroying non-NULL m_pMainWnd\n"
and
"Warning: Temp map lock count non-zero (%ld).\n",
The aim of this is to create a dialog box that allows a user to input car specifications and a track so a lap time can be calculated.
Source File of Main Dialogue box:
#define _WIN32_WINNT 0x0601
// LapTimeSim.cpp : implementation file
//
#include <iostream>
#include "stdafx.h"
#include "LapTimeSim.h"
#include "afxdialogex.h"
#include "SecondDlg.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
// LapTimeSim dialog
IMPLEMENT_DYNAMIC(LapTimeSim, CDialogEx);
LapTimeSim::LapTimeSim(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG1, pParent)
{
}
LapTimeSim::~LapTimeSim()
{
}
void LapTimeSim::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(LapTimeSim, CDialogEx)
ON_BN_CLICKED(IDC_CAR, &LapTimeSim::OnBnClickedCar)
ON_BN_CLICKED(IDGO, &LapTimeSim::OnBnClickedGo)
ON_BN_CLICKED(IDC_TRACK, &LapTimeSim::OnBnClickedTrack)
END_MESSAGE_MAP()
// LapTimeSim message handlers
void LapTimeSim::OnBnClickedCar()
{
// TODO: Add your control notification handler code here
CSecondDlg Dlg;
Dlg.DoModal();
}
void LapTimeSim::OnBnClickedGo()
{
// TODO: Add your control notification handler code here
}
void LapTimeSim::OnBnClickedTrack()
{
// TODO: Add your control notification handler code here
}
A Large Header; Header file of main dialog box
==============
#pragma once
// LapTimeSim dialog
class LapTimeSim : public CDialogEx
{
DECLARE_DYNAMIC(LapTimeSim)
public:
LapTimeSim(CWnd* pParent = NULL); // standard constructor
virtual ~LapTimeSim();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOG1 };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedCar();
afx_msg void OnBnClickedGo();
afx_msg void OnBnClickedTrack();
};
Source File of Main Dialogue box:
// SecondDlg.cpp : implementation file
//
#define _WIN32_WINNT 0x0601
#include <iostream>
#include "stdafx.h"
#include "LapTimeSim.h"
#include "afxdialogex.h"
#include "SecondDlg.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CSecondDlg dialog
IMPLEMENT_DYNAMIC(CSecondDlg, CDialog)
CSecondDlg::CSecondDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_DIALOG2, pParent)
{
}
CSecondDlg::~CSecondDlg()
{
}
void CSecondDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CSecondDlg, CDialog)
END_MESSAGE_MAP()
// CSecondDlg message handlers
A Large Header; Header file of second dialog box
==============
#pragma once
// CSecondDlg dialog
class CSecondDlg : public CDialog
{
DECLARE_DYNAMIC(CSecondDlg)
public:
CSecondDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CSecondDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOG2 };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
};
Source File : appmodule.cpp writeen by the wizard
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "sal.h"
/////////////////////////////////////////////////////////////////////////////
// export WinMain to force linkage to this module
extern int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow);
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
#pragma warning(suppress: 4985)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
/////////////////////////////////////////////////////////////////////////////
// initialize app state such that it points to this module's core state
BOOL AFXAPI AfxInitialize(BOOL bDLL, DWORD dwVersion)
{
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
pModuleState->m_bDLL = (BYTE)bDLL;
ASSERT(dwVersion <= _MFC_VER);
UNUSED(dwVersion); // not used in release build
#ifdef _AFXDLL
pModuleState->m_dwVersion = dwVersion;
#endif
#ifdef _MBCS
// set correct multi-byte code-page for Win32 apps
if (!bDLL)
_setmbcp(_MB_CP_ANSI);
#endif //_MBCS
return TRUE;
}
// force initialization early
#pragma warning(disable: 4074)
#pragma init_seg(lib)
#ifndef _AFXDLL
void AFX_CDECL _AfxTermAppState()
{
// terminate local data and critical sections
AfxTermLocalData(NULL, TRUE);
AfxCriticalTerm();
// release the reference to thread local storage data
AfxTlsRelease();
}
#endif
#ifndef _AFXDLL
char _afxInitAppState = (char)(AfxInitialize(FALSE, _MFC_VER), atexit(&_AfxTermAppState));
#else
char _afxInitAppState = (char)(AfxInitialize(FALSE, _MFC_VER));
#endif
/////////////////////////////////////////////////////////////////////////////
Source File - Wizard code coming up with warning:
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "sal.h"
/////////////////////////////////////////////////////////////////////////////
// Standard WinMain implementation
// Can be replaced as long as 'AfxWinInit' is called first
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT(hPrevInstance == NULL);
int nReturnCode = -1;
CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
// AFX internal initialization
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
goto InitFailure;
// App global initializations (rare)
if (pApp != NULL && !pApp->InitApplication())
goto InitFailure;
// Perform specific initializations
if (!pThread->InitInstance())
{
if (pThread->m_pMainWnd != NULL)
{
TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
goto InitFailure;
}
nReturnCode = pThread->Run();
InitFailure:
#ifdef _DEBUG
// Check for missing AfxLockTempMap calls
if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
{
TRACE(traceAppMsg, 0, "Warning: Temp map lock count non-zero (%ld).\n",
AfxGetModuleThreadState()->m_nTempMapLock);
}
AfxLockTempMaps();
AfxUnlockTempMaps(-1);
#endif
AfxWinTerm();
return nReturnCode;
}
/////////////////////////////////////////////////////////////////////////////
(See my answer to this SO: Cannot create main window?, it looks very similar to your issue)
If you have created this project with wizard then you should also have source files for CWinApp implementation. If its some other kind of wizard generated application then you still should somewhere have an InitInstance like method. with following lines of code:
LapTimeSim dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
(I assume here that LapTimeSim is your main entry dialog). If you remove above lines, you will get the exact same trace and behaviour as in your description (I checked this locally).
So you should check if you have by mistake removed them, or recreate project from MFC template wizard.
This question already has answers here:
When does invoking a member function on a null instance result in undefined behavior?
(2 answers)
Closed 7 years ago.
I'm trying to understand code below.
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class A
{
public :
void Test();
};
void A::Test()
{
ShowMessage("Hello");
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
A *x;
x->Test();
}
I expect EAccessViolation error,when I call the Test method.
How does work without the x assign ?
How does work without the x assign ?
In theory, the posted code is cause for undefined behavior.
In practice, it works some times because A::Test() does not depend on any member data. It is not guaranteed to work in every platform.
New to C++ so sorry if this is a basic question! I am used to Java (oh yess! so easy).
My function below addMessages is called from another file, it will then actually run __fastcall TfrmRunning::Add(). As i could not get this working from the other file. the add is part of the TdrmRunning object)
How do I get the add messages to call the Add function?
This is from Running.cpp
void __fastcall TfrmRunning::Add()
{
lbMessages->Items->Add("Application Started at ");
}
//This is called from another file as i could not get the above function working
void addMessages(){
TfrmRunning::Add(); // this does not work
}
My Header file (Running.H)
class TfrmRunning : public TForm
{
__published: // IDE-managed Components
TImage *imgLogo;
TLabel *lblCopyRight;
TLabel *lblTitle;
TButton *btnExit;
TButton *btnViewType;
TListBox *lbMessages;
void __fastcall btnExitClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Add();
private: // User declarations
public: // User declarations
__fastcall TfrmRunning(TComponent* Owner);
};
void addMessages();
Add() isn't a static function of TfrmRunning.
You'll need an object of type TfrmRunning to invoke it.
Try using
TObjetct *asd;
Add(asd);