Solved

BCB5 Stack overflow problem

Posted on 2007-03-19
2
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

632 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