Solved

BCB5 Stack overflow problem

Posted on 2007-03-19
2
293 Views
Last Modified: 2013-11-17
I am trying to pass a parameter via the constructor of a TForm but I get a Stack overflow error ?

I have used this method before but not with an integer as a parameter and have had no problems.

Any ideas wy I get the error ? The error is easily demonstrated by the following trivial code:

//header file unit1.h
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:      // IDE-managed Components
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
private:      // User declarations
public:            // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
// end of unit1.h

// unit1.cpp file
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TForm2 *f;

    f = new TForm2(NULL,2);
    delete f;
}
//---------------------------------------------------------------------------
// end of unit1.cpp file

//unit2.h file
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:      // IDE-managed Components
private:      // User declarations
public:            // User declarations
    __fastcall TForm2(TComponent* Owner,int n=0);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
//end of unit2.h file

//unit2.cpp file
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner,int n)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
// end of unit2.cpp file



0
Comment
Question by:alcindor
2 Comments
 
LVL 2

Accepted Solution

by:
rmmci earned 500 total points
ID: 18756172
when TForm 2 is created it calls
#pragma option push -w-inl
/* TCustomForm.Create */ inline __fastcall virtual TForm(Classes::TComponent* AOwner) : TCustomForm(AOwner) { }
from Forms.hpp and your getting the Int overflow by adding the int in the creation of
__fastcall TForm2::TForm2(TComponent* Owner,int n)
    : TForm(Owner)
causeing the stack over flow.
A better solution would be to pass via class in the public portion of the class.
0
 
LVL 2

Author Comment

by:alcindor
ID: 18778175
Hello rmmci,
My solution was to pass an int * as the argument which doesn't give the stack overflow and works just fine. I have used this method several times before but always passing a pointer as the argument (usually a pointer to a structure).
The points are yours, I am curious as to why it is ok to use a pointer argument but not an int ?

Thanks for your input,

Roger
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now