0

Hi Just want to know if i delete a global ptr will this lead to memory leak

SaurabhS
  • 633
  • 1
  • 7
  • 18
  • 1
    This is a very unusual question. You shouldn't ever have to "delete a global pointer." You should be using either smart pointers (or some other form of container) which handles the deletion for you so that you don't have to worry about it. – James McNellis Feb 24 '11 at 11:55

5 Answers5

6

No, but it can lead to a dangling pointer if there're other pointers to the object you delete. Memory leaks is when you have no pointers to an allocated object, so deleting a pointer will not cause a memory leak by itself.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • I think your answer could as well be "YES, eventually" :) I think this is a very good (although hard) way to learn about memory leaks in practice – davka Feb 24 '11 at 11:24
  • @davka - you mean a mem leak via useless pointers to freed memory? – Dennis Feb 24 '11 at 11:27
  • @Dennis: no, you are right, I should correct myself. This will not cause memory leaks, but can cause segmentation fault or memory corruption when it is accessed from other parts of the program, as explained in this answer. You can (and should), of course, set this pointer to 0 after `delete`, but such design in general is very error-prone – davka Feb 24 '11 at 11:38
  • @davka: From wikipedia..."A memory leak, in computer science (or leakage, in this context), occurs when a computer program consumes memory but is unable to release it back to the operating system." What "sharptooth" describes, is therefore not a memory leak, but -as he says- "dangling pointer(s)". – AudioDroid Feb 24 '11 at 11:39
  • @AudioDroid: you are right, see my answer to @Dennis. My intention was not to memory leaks but to memory problems in general. It was kind of a way to say "don't go there!". I would've deleted the comment, but that would leave "dangling comments", like yours... :) – davka Feb 24 '11 at 11:45
0

No but it could easily lead to a hanging pointer.

If your pointer is global it should probably just stick around until the end of execution.

Dennis
  • 3,683
  • 1
  • 21
  • 43
0

Deleting global pointer cannot lead to memory leak but can invalidate other pointers pointing to the same memory (making them "dangling").

char* g_p = new char[10]; 

void foo()
{
    delete[] g_p;
    g_p = 0; 
}

int main()
{
   char* p = g_p;
   foo();            
   // p still points to memory that has been deallocated 
   char ch = *p; // using dangling pointer - undefined behaviour
   return 0;
}

My (incorrect) example before edit (I have left it so posted comments make sense):

One typical case when deleting global pointer can lead to memory leak:

char* g_p = new char[10]; // allocate memory block 1

void foo()
{
   char* p = new char[10]; // allocate memory block 2
   g_p = p;
}

int main()
{
   foo();
   delete[] g_p; // free memory block 2
   return 0;
   // memory leak: memory block 1 has not been deallocated
}

Global pointer has been assigned with another value in foo so we lost the track of memory block 1 - there is no variable that keeps its starting address so we cannot free it. It is very difficult to track who and when changes global variables and this is one of the reasons why they should be avoided.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • Good answer, but really is it not a case that the memory leak is due to the non-deletion of the pointer p? – Dennis Feb 24 '11 at 11:31
  • in your example reason of leak is not deleting g_p but assigning p to g_p without deleting g_p. – mentat Feb 24 '11 at 11:32
  • @Dennis Well, the point is which address is passed to delete operator, not through which pointer - `delete[] g_p` frees memory pointed by `p` in `foo`. Memory allocated in first line still remains unreleased. – Bojan Komazec Feb 24 '11 at 11:47
  • @Koray Exactly! And that would be the solution how to fix this memory leak. – Bojan Komazec Feb 24 '11 at 11:49
  • @James (and @Dennis) I understand your point and will edit my answer. – Bojan Komazec Feb 24 '11 at 12:10
0

delete-int a global pointer value will not result in a memory leak, but could result to an aliased dangling pointer.

Imagine

int * ptr;

int main()
{
     ptr = new int;
     int * alias = ptr;
     delete ptr;
     *alias = 5; // Undefined Behaviour
     return 0;
}

Using this memory location now will result in Undefined Behaviour. When two pointers "point" to the same location, this situation is called Aliased Pointer

It should be better to design your system in a better way and try to not use global variables. However, the same problem could still exist even in a local function body.

Also, it should be noted that ptr when your program starts execution is automatically initialized to 0 (cause of its storage class), so if you try to do a delete ptr on the first line of your program nothing bad will happen (and yes, global variables are initialized before execution of main() starts).

There was a somehow maybe relevant post 1-2 days ago, and I think (since it is C++) that you might find it interesting and helpful. Take a look

Community
  • 1
  • 1
0

There is no instance in which the deletion of a pointer, global or otherwise, will definitely lead to a memory leak per se. However, there are a few instances that may result in a memory leak. Notably, invoking a delete-expression on a pointer to memory not allocated with the corresponding new-expression, leads to undefined behavior (for example, a memory leak), which should always be avoided.

On some compilers,

int * p = new int[30];

int main()
{
  delete p; // undefined behavior
}

the delete-expression may end up only freeing memory equivalent to one int. Since p points to an array, delete[] p; should've been used instead.

decltype
  • 1,591
  • 8
  • 12