I am having the following problem: I have written a console application that needs to allocate memory. I am now debugging the program. I do not have any problems the first time I debug. Bu then, when I find an error or the program crashes and the debugging terminates, I can no longer debug because when I start the program again, it crashes at the first memory allocation instruction. I have to restart my computer to be able to debug the program again!! I imagine that this happens because as the program did not terminate correctly, the deallocation instructions were not reached, so the memory stays allocated. I have never had this problem with the other versions of Visual Studio I have used. Or could this be because of Vista? Could someone help me, please? Thanks!!

Recommended Answers

All 11 Replies

Are you sure you stopped debugging correctly? Not paused or something?
What debugger are you using?
Perhaps you can show some code

Are you sure you stopped debugging correctly? Not paused or something?
What debugger are you using?
Perhaps you can show some code

I am stopping the debugger using "terminate all" option. Sometimes is the debugger itself that stops the execution when the program crashes.
As an example, I am debugging a procedure that has the following memory allocations:

typedef struct{
    //arco (a,b)
    int a;
    int b;
    double custos;
}arco;


arco *custos;
double *c;
int *peso,*etiqueta,corrente,menor,escolhido;
int j,cont;

int arcos_total=0;

try{
    custos=new arco[(TEMP+1)^2];
    for(int t=1;t<=TEMP;t++){
        for (int t1=t+1;t1<=TEMP+1;t1++){
            custos[arcos_total].a=t;
            custos[arcos_total].b=t1;
            custos[arcos_total].custos=FLT_MAX;
            arcos_total++;
        }
    }//for t

    c=new double[TEMP+2];

    peso=new int[TEMP+2];
    etiqueta=new int[TEMP+2];
}//try
catch(...){
    cout << "Problemas no calculo caminhos";
    exit(-1);
}//catch

The first time the program executes, this allocations are made. But I have a problem inside this procedure, that I could not yet solve, so the procedure terminates with an error.
The second time I execute the program, I always end up in the catch instructions...

Thank you so much for your attention!!

As what is TEMP defined? custos=new arco[(TEMP+1)^2]; Do you realise that '^' means XOR and not 'to the power' ?

What debugger are you using?

Also please read this about code tags before posting your code again

[edit] Whoohoo 1000 posts for me :)

Sorry, there must have been a problem with the copy paste...
Here is the code (I hope now I am doing things properly, sorry...)

I am using visual studio 2008, and I am using the default debugger.

TEMP is an integer variable that belongs to the class.

Thanks again!

try{
	custos=new arco[(TEMP+1)*10];
	for(int t=1;t<=TEMP;t++){
		for (int t1=t+1;t1<=TEMP+1;t1++){
			custos[arcos_total].a=t;
			custos[arcos_total].b=t1;
			custos[arcos_total].custos=FLT_MAX;
			arcos_total++;
		}
	}//for t

	c=new double[TEMP+2];

	peso=new int[TEMP+2];
	etiqueta=new int[TEMP+2];
}//try
catch(...){
	cout << "Problemas no calculo caminhos";
	exit(-1);
}//catch

You are running in memory that isn't yours.
Let's say that TEMP = 200; Then this line custos=new arco[(TEMP+1)*20]; will create (200+1)*20 = 4020arco's

But then you do :

for(int t=1;t<=TEMP;t++)
{
    for (int t1=t+1;t1<=TEMP+1;t1++)
    {
           custos[arcos_total].a=t;
           custos[arcos_total].b=t1;
          custos[arcos_total].custos=FLT_MAX;
          arcos_total++;
    }
}

Which will result that arcos_total becomes far greater then 4020 => memory exception => crash

You are running in memory that isn't yours.
Let's say that TEMP = 200; Then this line custos=new arco[(TEMP+1)*20]; will create (200+1)*20 = 4020arco's

But then you do :

for(int t=1;t<=TEMP;t++)
{
    for (int t1=t+1;t1<=TEMP+1;t1++)
    {
           custos[arcos_total].a=t;
           custos[arcos_total].b=t1;
          custos[arcos_total].custos=FLT_MAX;
          arcos_total++;
    }
}

Which will result that arcos_total becomes far greater then 4020 => memory exception => crash

Thanks for your answer, but that is not the problem. As I am only testing the code I am working with TEMP=5, so arcos_total=10, far less than 5*20. In the final version, the number of elements in this vector will be calculated correctly as the sum of the first elements of a succession .
Nevertheless, I think I have spotted the problem... in the procedure that calls this one. I am referencing a pointer that was not allocated... I have fixed this and my problem is gone... is it possible that this problem in another procedure only appears doing damage some instructions later?
Thank you so much for your attention... This forum is great.

is it possible that this problem in another procedure only appears doing damage some instructions later?

Sure, especially with pointers that point nowhere ;)

So this is not your complete code? Are you deleting the memory that you allocated somewhere else in your program? Else you'll get horrible memory-leaks

Thank you so much for your attention... This forum is great.

No prob. And: Yes it is ;)

Sure, especially with pointers that point nowhere ;)

So this is not your complete code? Are you deleting the memory that you allocated somewhere else in your program? Else you'll get horrible memory-leaks

No prob. And: Yes it is ;)

This is not all the code. The program has, by now, more or less 2000 lines. I am deleting all the allocated memory. Thanks.

Hi.
Well...
First of all,I think that you had better show us definition of 'TEMP' ,and 'arco'.
I bother following code.

for(int t=1;t<=TEMP;t++){
        for (int t1=t+1;t1<=TEMP+1;t1++){
How much is value of arco ,if the value of the array 'arco' index ?
I think you had better describe following code.
for(int t=0;t<TEMP;t++){
     for(int t1=t+1;t1<TEMP+1;t1++){

Thanks!

Hi.
Well...
First of all,I think that you had better show us definition of 'TEMP' ,and 'arco'.
I bother following code.

for(int t=1;t<=TEMP;t++){
        for (int t1=t+1;t1<=TEMP+1;t1++){

How much is value of arco ,if the value of the array 'arco' index ?
I think you had better describe following code.

for(int t=0;t<TEMP;t++){
     for(int t1=t+1;t1<TEMP+1;t1++){

Thanks!

Hi. First of all I think the problem was allready solved
Second: if you post code please use code tags
Third: If you had read this thread you would have known the previous two
Last but not least: Welcome to Daniweb ;)

Hi. First of all I think the problem was allready solved
Second: if you post code please use code tags
Third: If you had read this thread you would have known the previous two
Last but not least: Welcome to Daniweb ;)

OK.
I see.
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.