Can't delete pointer!

May 30, 2012 at 6:37am
Hi! I'm using Visual C++, from the visual studio 2010 suite, and I'm having some troubles, I'm receiving an error, here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;


	int variable = 5;
	int * pLocal= &variable;
	int * pHeap = new int;
	*pHeap = 7;

	cout << "variableLocal: " << variable << endl;
	cout << "*pLocal: " << *pLocal << endl;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;
	pHeap = new int;
	*pHeap = 17;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap; 
	delete pLocal; //it fails here!!!!
	pLocal = new int;

	system("PAUSE");
	return 0;
}

I'm receiving a "Debug Assertion" Error.
it also says "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

Some Help?

Thanks in advance!
Last edited on May 30, 2012 at 6:42am
May 30, 2012 at 6:54am
1
2
3
	int variable = 5;
	int * pLocal= &variable;
	delete pLocal; //it fails here!!!! 


Of course it fails there. You don't have any business deleting that memory. It was not allocated with new.
May 30, 2012 at 7:00am
If you reverse lines 23 and 24 order then it will work :)
May 30, 2012 at 7:01am
> delete pLocal; //it fails here!!!!

You can, and need to, use delete pLocal if and only if pLocal contains a pointer returned as the result of a new

May 30, 2012 at 7:03am
It works!, but even if I allocate with new, it fails under debug configurations, not happening under release config.
May 30, 2012 at 7:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;


	int variable = 5;
	int * pLocal = new int;
	pLocal= &variable;
	int * pHeap = new int;
	*pHeap = 7;

	cout << "variable: " << variable << endl;
	cout << "*pLocal: " << *pLocal << endl;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;
	pHeap = new int;
	*pHeap = 17;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;
	delete pLocal;
	

	system("PAUSE");
	return 0;
}


I changed it to this, but the debug window says HEAP[Punteros.exe]: Invalid Address specified to RtlFreeHeap( 00390000, 0012FF74 )
Windows has triggered a breakpoint in Punteros.

Last edited on May 30, 2012 at 7:09am
May 30, 2012 at 7:07am
Also if I revert the lines 23 and 24, doesn't that produce a memory leak?
Last edited on May 30, 2012 at 7:08am
May 30, 2012 at 7:09am
1
2
3
	int * pLocal = new int;
	pLocal= &variable;
	delete pLocal;


Once again, pLocal does not point to memory allocated by new. You leaked that memory when you set pLocal to &variable.
May 30, 2012 at 7:14am
> but even if I allocate with new, it fails under debug configurations

This use of delete will not fail:
1
2
3
4
5
6
7
int main()
{
     int* pointer = new int(7) ;
     // use *pointer
     delete pointer ;
     // do not use the current value of pointer  
}


This use of delete will lead to undefined behaviour:
1
2
3
4
5
6
7
int main()
{
     int v = 7 ;
     int* pointer = &v ;
     // use *pointer
     delete pointer ; // undefined behaviour
}


http://msdn.microsoft.com/en-us/library/h6227113(v=vs.100).aspx



> it fails under debug configurations, not happening under release config.

That and its converse - "my code works correctly with a debug build, but fails with a release build" - is one of the most common manifestations of 'undefined behaviour'.
May 30, 2012 at 7:24am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;


	int variable = 5;
	int * pLocal = new int(variable);

	int * pHeap = new int;
	*pHeap = 7;

	cout << "variable: " << variable << endl;
	cout << "*pLocal: " << *pLocal << endl;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;
	pHeap = new int;
	*pHeap = 17;
	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;
	
	
	delete pLocal;
	

	system("PAUSE");
	return 0;
}


fixed, now I know, that, "variable" is not inside the free store, so I can't make a reference with the use of the "&" operand, it means that, everytime i need to allocate to the free store i should use the "new" operand?
May 30, 2012 at 7:47am
pLocal= &variable;
This line is valid, you are pointing to memory on the stack. Just set it to null when you are done with the pointer.
@cire
I dont see how it is a memory leak, a dangling pointer yes if variable goes out of scope.
May 30, 2012 at 8:36am
@cire
I dont see how it is a memory leak, a dangling pointer yes if variable goes out of scope.


The pointer and the variable had the same scope, but you don't see how it's a memory leak?

Tell me, after this:

1
2
	int * pLocal = new int;
	pLocal= &variable;


How do you free the memory that was allocated in the first line?
May 30, 2012 at 11:28am
so it leaks because it was allocated and never used? and, instead of that, we referenciate pLocal to "variable"'s address?
May 30, 2012 at 2:56pm
It was leaked because it was allocated and all reference to it was lost, leaving us unable to reclaim or use the memory.
May 30, 2012 at 3:50pm
@cire
I see whet you mean, I was looking at the original code where he didn't new pLocal.
Topic archived. No new replies allowed.