Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, I've been having a probl

Status
Not open for further replies.

nawaf

Programmer
Jun 11, 2001
12
0
0
KW
Hello,
I've been having a problem for quite a while, the problem started when I was coding a program few months ago, whenever I attempt to use the delete operator to delete dynamically allocated objects using new operator, an error message appears, the message says:
Microsoft Visual C++ Debug Library

Debug Error!

Program: .....
Module:
File: i386\chkesp.c
Line: 42

The value of ESP was not properly saved after a function call. This is usually the result of calling a function with one calling convention with a function pointer declared with a different calling convention.


I removed the delete operator, and the program worked well except for the memory leaks, and now, I'm having the same problem all over again, I'm coding a new program, and the same is hapenning again, would you tell me what is the problem please?!
the code where I delete the object is:


void CRegression::Replace(CChromosome new_chrom)
{
int index = 0;
for (int i = 1; i < POPULATION_SIZE; i++)
{
if (Population->GetQuality() <Population[index]->GetQuality())
index = i;
}
delete Population;

Population = & new_chrom;
}

and the code where I creaye the new object is:


CRegression::CRegression()
{
for (int i =0; i < POPULATION_SIZE; i++)
{
Population = new CChromosome();
Population->CreateRandom();
}
}

Thanks
Sincerely
Nawaf Arhamah
 
U r deleting the object population and thereafter trying to assign it some other address. This could possibly be one of the problem. however if it is very much required what u want to do, then, after deletion reassign the memory using new operator.

Hope that Helps,
SwapSawe s-)
 
yah, but I've already assigned it before I delete it, as you can see:
delete Population;
Population=& new_chrom;
but this isn't the problem, the program doesn't even reach to the statement after the delete, it stops there and displays the message, I don't see any logical error in the code, nor violation of the specifications of the C++, can you you see any?!
if yes, please tell me, if not, can you tell me what could have been went wrong in the program, and why this silly message is appearing? I've searched the MSDN, there is no info at all, just nothing!
I'm having a trouble, I need to allocate objects dynammically, but then I have to delete them, or the memory will be full with garabge objects, the number of these objects could be large, can you help me for god's sake, I'm really confused, is it a bug un the VC++?
or is it my mistake, something is wrong in the code? notice the code is :
void CRegression::Replace(CChromosome new_chrom)
{
int index = 0;
for (int i = 1; i < POPULATION_SIZE; i++)
{
if (Population->GetQuality() <Population[index]->GetQuality())
index = i;
}
delete Population;

Population = & new_chrom;
}

and the code where I creaye the new object is:


CRegression::CRegression()
{
for (int i =0; i < POPULATION_SIZE; i++)
{
Population = new CChromosome();
Population->CreateRandom();
}
}

but the index was not appearing because of the TGML, this may make a difference? I don't know, I'm disprit.
anyway, thanks for patience, I hope you read my message all the way up to here :)
thanks
Sincerely
Nawaf Arhama
 
Hi,

Try delete [] Population and see if it works.

abp
 
I meant delete [] Population .Sorry for the TGML
error.

abp
 
hi,
I've done what you said, the same error, nothing changed.
notice that in former programs, the same error was hapening, although I didn't use arrays.
 
Population = & new_chrom;
In this line you put addres of stack variable new_chrom not the one you have pass in argument line, this stack variable will be deleted on the exit from your function.
Just change next line:
void CRegression::Replace(CChromosome &new_chrom)
{
}
and it will work.
Regards.
 
What does
1. Population->GetQuality()<Population[index]->GetQuality() mean?
Population after < is an array or pointer CChromosome pointers but population before < is a pointer...
2. delete Population; which population are you deleting? Do you delete a pointer or an array of pointers, or a dinamically created object with index i? Can you say, what the role of index and of i are? John Fill
1c.bmp


ivfmd@mail.md
 
What I've had happen, and I'm not really sure why, is that VC++ doesn't like you to allocate memory with new in a constructor, and then delete it in a member function (or, for that matter, allocate in a member function and delete in a destructor). Try moving the memory allocation into an Initialize() function, and call that from the constructor, or just after construction, and see if that works...I know it has for me in similar situations.
 
By the way, I have an biological qiestion. Is there a population of [COLOR= blue]cromosomes[/color]? Nawaf, can you email to me the problem? John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top