Solved

BCB5 Stack overflow problem

Posted on 2007-03-19
2
294 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
modThree challenge 4 80
canBalance challenge 34 70
Execute multiple curl cmds with sleep and send output to file 10 89
Fibonacci challenge 11 112
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 …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now