Previous in Forum: Install Application - Access Denied   Next in Forum: Domino Printer A100+ Administrator Password
Close
Close
Close
20 comments
Rate Comments: Nested
Associate

Join Date: Aug 2010
Posts: 25

Why This Error Displayed: Insufficient Memory..

09/01/2010 1:19 AM

Hi,

I am modifying the program for a machine, and the code was wrote in C++ Builder 5.0(too old), using Access 2000 as database. Other info as below:

(1)In main form when starts up it will initialize the machine:

...

if( !Config->Initialize() && (InProduction || Config->ErrNum > 5) )
{
InfoScreen("Cannot Configure Station\n\n"+Config->ErrStr, false, true, false, "", clRed);
return;

}

...

InfoScreen() is something like a Messagebox(). The error "insufficient memory..." will be displayed occasionally(Note: just occasionally). I trace it(error comes from sentence "Config->Initialize()") and find it goes into:

(2) bool __fastcall TDataConfig::Initialize()
{
bool Result;
String S;
ErrNum = 0;
ErrStr = "No Error";
if(!ConfigDB->Connected){
try{
ConfigDB->Connected = true;
}catch(...){
ErrNum = 9;
ErrStr = "Failed to connect to DATA_CONFIG database! The file might be missing or corrupted!";
return false;
}

}

...

(3) In the *h file has declaration:

...

TDatabase *ConfigDB;

...

In the TDatabase construction function has nothing related with ConfigDB.

Am I said clearly? Other related info will be provided if possible.

I am not sure this class is safe or not. Can someone give me some tips on this issue?

Register to Reply
Pathfinder Tags: Insufficient memory
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Power-User
Hobbies - Automotive Performance - New Member Hobbies - CNC - New Member

Join Date: Jun 2008
Location: Long Island, NY
Posts: 326
Good Answers: 1
#1

Re: Why This Error Displayed: Insufficient Memory..

09/01/2010 4:29 PM

I would suggest posting this question on comp.lang.c++.

__________________
It is better to fail in originality than to succeed in imitation.
Register to Reply Score 1 for Good Answer
Associate

Join Date: Aug 2010
Posts: 25
#4
In reply to #1

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 4:23 AM

Thanks.

How to find the place?

Register to Reply
Power-User
Hobbies - Automotive Performance - New Member Hobbies - CNC - New Member

Join Date: Jun 2008
Location: Long Island, NY
Posts: 326
Good Answers: 1
#6
In reply to #4

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 5:10 PM

http://groups.google.com/group/comp.lang.c++/topics?hl=en

__________________
It is better to fail in originality than to succeed in imitation.
Register to Reply
Guru

Join Date: Oct 2009
Posts: 1460
Good Answers: 30
#2

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 3:35 AM

I'm sorry to say that at this range it is quite impossible to sort out your problem. We do not even know if the problem is software or hardware (if you are working on an old machine it might be a RAM problem). In software there are any number of causes, related to not clearing up memory used by variables, and to using pointers which are not properly initialised, and so point to undefined memory areas. It can only get worse if you are modifying someone else's program, particularly if it is not properly commented. Any variable which is created by new() should also be disposed of by delete() within the appropriate scope. I have written a large project with a database in C++ Builder 5, so it is a perfectly adequate tool for the job, but the only way you will get to the bottom of this is to show your whole code (not just the tiny fragment you have shown us) to a programmer. You may also find you need to adjust some settings in the compiler to allow for a bigger heap and stack.

Register to Reply Score 1 for Good Answer
Associate

Join Date: Aug 2010
Posts: 25
#3
In reply to #2

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 4:21 AM

Hi, phph001,

You are great, thank you.

The code is very large which started from 2002 and transfered for several guys.

I know little about C++ Builder 5.0, so could you help to show me how to change the heap and stack size in it? In "Project Opitions/Linnker/PE file option" or in "Project Opitions/Tasm/Sections"?

Thank you again.

Register to Reply
Guru

Join Date: Oct 2009
Posts: 1460
Good Answers: 30
#5
In reply to #3

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 9:36 AM

Sorry, I have given up on C++ and it is no longer on my machine, but you should find the heap and stack settings of one of the Project Options sub-menus. It may well be Linker, but not Tasm (which is for assembly language).

Register to Reply Score 1 for Good Answer
Associate

Join Date: Aug 2010
Posts: 25
#7
In reply to #5

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 8:32 PM

Thank you. phph001.

Register to Reply
2
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#8

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 9:46 PM

The piece of code you provided in not enough. Your question is also not clear. However, here is what I found:

TDatabase is a class and ConfigDB is the pointer of the TDatabase (it is defined in the header file)

Connected is a Boolean variable of the class TDatabase.

It not clear the pointer Config is of which class but most probably it is the pointer of TDataConfig class.

If error occurs during the initializing, the system gives the error and the program exits (retun in main function is same as exit).

What do you mean by 'construction function'? Did you mean Constructor of TDatabase?

Debugging or understanding programming code needs the whole piece of code. With this short piece of code, I (even any other expert programmers) can not tell much more than this.

Note: In this piece of code, the variables (such as ConfigDB and Confif) are started with capital letter. This is not a good practice of programming convention.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply Good Answer (Score 2)
Associate

Join Date: Aug 2010
Posts: 25
#9
In reply to #8

Re: Why This Error Displayed: Insufficient Memory..

09/02/2010 11:17 PM

Hi, msamad,

Thank you very much.

Maybe I am not stated clearly.

1) I found in main form *.cpp file has declaration:TDataConfig* Config;

2) I meant 'Construction function' equals Constructor of class TDataConfig, not TDatabase. Previously I said was not correct. Sorry for this.

Besides above,

(1) in TDataConfig head file:

class TDataConfig : public TDataModule
{
__published: // IDE-managed Components
TDatabase *ConfigDB;
TQuery *QueryCONFIG;
TUpdateSQL *UpdateSQLCONFIG;
private:

...

public:

int LineNumber;

int LinePosition;


int __fastcall Read(String Item, int Default);

......
public: // User declarations
__fastcall TDataConfig(TComponent* Owner);
__fastcall TDataConfig();

bool __fastcall Initialize();

......

(2) in TDataConfig cpp file, has a Read function:

int __fastcall TDataConfig::Read(String Item, int Default)
{
String S = "";
int Result;
S = "";
if( QueryCONFIG->RecordCount != 0 )
if(QueryCONFIG->FindField(Item) != NULL)
S = QueryCONFIG->FindField(Item)->Value;
Result = S.ToIntDef(Default);
return Result;

}

(3) in TDataConfig cpp file, Initialize function(displayed again):

bool __fastcall TDataConfig::Initialize()
{
bool Result;
String S;
ErrNum = 0;
ErrStr = "No Error";
if(!ConfigDB->Connected){
try{
ConfigDB->Connected = true;
}catch(...){
ErrNum = 9;
ErrStr = "Failed to connect to SEAL_CONFIG database! The file might be missing or corrupted!";
return false;
}
}

QueryCONFIG->Active = false;
S = "select * from SEAL_CONFIG_NEW where SN = '" + SN + "'";
QueryCONFIG->SQL->Clear();
QueryCONFIG->SQL->Add(S);
QueryCONFIG->Active = true;

LineNumber = Read("LineNumber",0);
LinePosition = Read("LinePosition",0);

......

By the way, this program runs on a PC with Pentium III, 256M RAM, also has a virusscan software--McAfee.

This error occurs occasionally.

Any suggestion?

Thank you again.

Register to Reply
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#10
In reply to #9

Re: Why This Error Displayed: Insufficient Memory..

09/03/2010 2:22 PM

I would recommend you first to see the memory leak. This type of error generally appears in the case where memory is not properly released after it is dynamically allocated. If the program runs for a while and after certain time, the error appears and it works ok after it is restarted – the most probably cause is the memory leak.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply Score 1 for Good Answer
Associate

Join Date: Aug 2010
Posts: 25
#11
In reply to #10

Re: Why This Error Displayed: Insufficient Memory..

09/05/2010 8:14 PM

Hi, Msamad,

Thank you.

Do you have any general solution on this memory leak? I know little about this.

Register to Reply
2
Guru

Join Date: Oct 2009
Posts: 1460
Good Answers: 30
#12
In reply to #11

Re: Why This Error Displayed: Insufficient Memory..

09/06/2010 5:04 AM

The general principle is to assign memory for working purposes, and to release it when no longer required. Some languages (Java, Basic) have "garbage disposal" routines to clear no-longer-used memory areas, but C and C++ do not. In C, memory is assigned with malloc() or calloc() and can be reallocated with realloc(). It must, however, eventually be released using free(). These routines can also be used in C++, but it is better to use new() to create new variables. A particular catch is that if you create a single object with new(), you must release it with delete(), but if you create an array, you must release it with delete[](). The compiler does not catch an error if you use delete() on an array, but you will get memory leaks. I know it is hard, but you really have to examine the creation of every temporary variable to see whether it is properly disposed of when it is no longer needed. This is one of the reasons why other people's amateur programming is so difficult to salvage.

Register to Reply Good Answer (Score 2)
2
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#13
In reply to #12

Re: Why This Error Displayed: Insufficient Memory..

09/06/2010 3:06 PM

I think phph001 has provided enough and good information about the C++ memory leak. I just added few things:

(1) When the memory is allocated dynamically (generally for creating an instance of class using new), the memory is assigned to the program by the operating system and the address of the memory is stored in a pointer. The memory can be accessed through the pointer.

(2) If another memory (or set of memory) is allocated and its address is assigned to the same pointer without releasing the previous memory, then memory leak occurs – these memories are not released back to the operating system.

(3) When a program runs for a while and the memory keeps leaking, the operating system gets shortage of memory and finally the insufficient memory problem happens.

(4) Look into your code and see the cases of 'new' function is used and the address is assigned to a pointer, make sure the previous memory assigned to the pointers are released using 'delete'. Generally, the memories allocated in a method (with assigning its address to a local pointer variable) are released at the end of the method, if they are not required to access any more.

(5) When the program is terminated, all allocated memories (including the leaking ones) are released back to the operating system. This is the reason the problem disappears after the restarting of the program.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply Good Answer (Score 2)
Associate

Join Date: Aug 2010
Posts: 25
#14
In reply to #13

Re: Why This Error Displayed: Insufficient Memory..

09/06/2010 8:20 PM

Hi, Thank you both.

I will check the code again to find 'new-delete' words paired or not.

Register to Reply
Associate

Join Date: Aug 2010
Posts: 25
#15
In reply to #13

Re: Why This Error Displayed: Insufficient Memory..

09/16/2010 11:28 PM

Hi, msamad,

I have checked the source code, and find some suspicious place. Now I list 2 place of them. Can you help me how to modify them?

---------------------------------------------------------------------------------

1) In MainForm.h, I find the definition:

TIOButton *DIn[MAX_IO];

TIOButton *DOut[MAX_IO];

---------------------------------------

and in MainForm.cpp I find:

void __fastcall TForm1::CreateIOButtons()
{
int i = 0;
int Top = 10;
int Left = 12;
for( ; i<22; ++i )
{
//Inputs
DIn[i] = new TIOButton(this);
DIn[i]->Parent = TabInputs1;
DIn[i]->Left = Left;
DIn[i]->Top = Top;
//Outputs
DOut[i] = new TIOButton(this);
DOut[i]->Parent = TabOutputs1;
DOut[i]->Left = Left;
DOut[i]->Top = Top;
Top += 40;
if( i == 10 )
{
Top = 10;
Left = 404;
}
}
Top = 10;
Left = 12;
for( ; i<44; ++i )
{
//Inputs
DIn[i] = new TIOButton(this);
DIn[i]->Parent = TabInputs2;
DIn[i]->Left = Left;
DIn[i]->Top = Top;
//Outputs
DOut[i] = new TIOButton(this);
DOut[i]->Parent = TabOutputs2;
DOut[i]->Left = Left;
DOut[i]->Top = Top;
Top += 40;
if( i == 32 )
{
Top = 10;
Left = 404;
}
}
Top = 10;
Left = 12;
for( ; i<66; ++i )
{
//Inputs
DIn[i] = new TIOButton(this);
DIn[i]->Parent = TabInputs3;
DIn[i]->Left = Left;
DIn[i]->Top = Top;
//Outputs
DOut[i] = new TIOButton(this);
DOut[i]->Parent = TabOutputs3;
DOut[i]->Left = Left;
DOut[i]->Top = Top;
Top += 40;
if( i == 54 )
{
Top = 10;
Left = 404;
}
}

}

----------------------------------------

You can see from above statement, there are 6 "new" and I can't find any delete in the whole program.

How can I modify this part?

-------------------------------------------------------------------------------

2) I also find some "new" in other class:

In FOFLightTower.cpp has "new" like:

// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TFOFLightTower *)
{
new TFOFLightTower(NULL);

}

...

__fastcall TFOFLightTower::~TFOFLightTower()
{

}

How do you think of this type "new"?

Thank you very much.

Register to Reply
2
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#16
In reply to #15

Re: Why This Error Displayed: Insufficient Memory..

09/17/2010 9:18 PM

There is the possibility of memory leak from this code. When the CreateIOButtons() is called, the memory for 2x66 TIOButton is allocated and they are assigned to the array of pointers Din[0] …Din[65] and Dout[0]…Dout[65]. If CreateIOButtons() is called again, there are another 2x66 TIOButton are allocated and they are also stored in the same pointers and the previous memories are leaked. This keeps happening for each successive call of CreateIOButtons() if the previously allocated memory is not released. The memory leak depends on the nature of call of CreateIOButtons() in the program.

To resove this, you can create a DeleteIOButtons() method in which the allocated TIOButton are deleted and call it before the each call of CreateIOButtons(). The DeleteIOButtons() can be like

void __fastcall TForm1::DeleteIOButtons()
{
int i = 0;
for(; i<22; ++i )
{

if (Din[i] != NULL)

delete Din[i];

if (Dout[i] != NULL)

delete Din[i];

}

Or simply add this piece of code in CreateIOButtons(),

void __fastcall TForm1::CreateIOButtons()
{
int i = 0;
int Top = 10;
int Left = 12;

// Release previously allocated memory

int j =0;

for(; j<22; ++j )
{

if (Din[j] != NULL)

delete Din[j];

if (Dout[j] != NULL)

delete Din[j];

}


for( ; i<22; ++i )
{
//Inputs
DIn[i] = new TIOButton(this);
DIn[i]->Parent = TabInputs1;
DIn[i]->Left = Left;
DIn[i]->Top = Top;

-

-

I am away from programming for long time and this is what I can see and suggest. However, you can consult with a good programmer or try this code in the test environment.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply Good Answer (Score 2)
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#17
In reply to #16

Re: Why This Error Displayed: Insufficient Memory..

09/17/2010 9:42 PM

A correction:

In the for loop, replace 22 with 66.

Please come up with the result.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply
Associate

Join Date: Aug 2010
Posts: 25
#18
In reply to #17

Re: Why This Error Displayed: Insufficient Memory..

09/20/2010 4:14 AM

Thank you, Msamad,

Because of other issue I have to postpone this project for 2 weeks.

Anyway, I will give you the last result after I get it.

Thank you again.

Register to Reply
Associate

Join Date: Aug 2010
Posts: 25
#19
In reply to #17

Re: Why This Error Displayed: Insufficient Memory..

10/11/2010 12:55 AM

Hi, Msamad,

Thank you.

The program is ok now.

Register to Reply
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#20
In reply to #19

Re: Why This Error Displayed: Insufficient Memory..

10/11/2010 9:57 PM

I am glad to know that you got helped.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Register to Reply
Register to Reply 20 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

Economist (2); msamad (6); phph001 (3); yuzaihuan (9)

Previous in Forum: Install Application - Access Denied   Next in Forum: Domino Printer A100+ Administrator Password

Advertisement