GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Dec-2005, 09:24
gaoanyu gaoanyu is offline
New Member
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 26
gaoanyu is on a distinguished road

Memory de-allocation during debugging


Dear experts,

It has happened many times to me that during debugging, the program experience some exception that asks the me to terminate debugging. However, if this happens after I have just allocated memory to an object but did not reach the line of de-allocation, what will the debugger do? Will it de-allocate the memory itself? The debugger I am using is Visual C++. NET and the OS I am on is Windows XP. What should I do if it does not automatically de-allocate the memoty? Thanks in advance!

Kind Regards,
David
  #2  
Old 13-Dec-2005, 09:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Memory de-allocation during debugging


Quote:
Originally Posted by gaoanyu
Dear experts,
Sucking up won't get you better help

Quote:
Originally Posted by gaoanyu
It has happened many times to me that during debugging, the program experience some exception that asks the me to terminate debugging. However, if this happens after I have just allocated memory to an object but did not reach the line of de-allocation, what will the debugger do? Will it de-allocate the memory itself? The debugger I am using is Visual C++. NET and the OS I am on is Windows XP.
Unless I'm mistaken (and someone here will correct me if I am), programs run in their own space. When the program terminates, the allocated memory will be released back to the system. The problem with memory leaks (not releasing allocated memory) is that while the program runs it eats up available memory slowing the computer and probably crashing itself.

Quote:
Originally Posted by gaoanyu
What should I do if it does not automatically de-allocate the memoty?
Reboot.
__________________

Age is unimportant -- except in cheese
  #3  
Old 14-Dec-2005, 15:01
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Memory de-allocation during debugging


Basically, windows XP will de-allocate the memory allocated by your application.

But, and this is a big but : Don't count on it !
I personally coded a memory handling set of functions, that allow me to keep an eye on my allocations and free them when needed.
If you worrie about your applications crashing and leaving "left-overs" behind, then perhaps you could build yourself such a module.

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #4  
Old 15-Dec-2005, 06:56
gaoanyu gaoanyu is offline
New Member
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 26
gaoanyu is on a distinguished road

Re: Memory de-allocation during debugging


Thanks for your posts, guys.
WaltP, actually, I am thinking of ways of de-allocating memory without reboot, because I always open a few applications and don't want to close and open them again. Any suggestions?
kobi hikri, do you mean the set of functions like _CrtDumpMemoryLeaks()? Or do you mean you have written a set of functions that will independently save the memory state information before debugging and after forced termination? So that it knows which memory to de-allocate? Can you elaborate? Thanks!
  #5  
Old 15-Dec-2005, 07:26
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Memory de-allocation during debugging


Quote:
Originally Posted by gaoanyu
kobi hikri, do you mean the set of functions like _CrtDumpMemoryLeaks()? Or do you mean you have written a set of functions that will independently save the memory state information before debugging and after forced termination? So that it knows which memory to de-allocate? Can you elaborate? Thanks!

Hey.

I mean I wrote my own versions of malloc, calloc, realloc, and free.
My versions use the original functions, but keep track of the memory allocated and de-allocated.
Let me know if you have interest in this code (there are some bugs that I didn't fix yet, with the reallocation process - So I'd prefer not post it).

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #6  
Old 15-Dec-2005, 07:27
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Memory de-allocation during debugging


Kobi,

Quote:
Originally Posted by Kobi
Let me know if you have interest in this code (there are some bugs that I didn't fix yet, with the reallocation process - So I'd prefer not post it).
I am very much interested!
Also, Senior members can help us fix the bug.


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 15-Dec-2005, 07:32
gaoanyu gaoanyu is offline
New Member
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 26
gaoanyu is on a distinguished road

Re: Memory de-allocation during debugging


Hi, Kobi,

Yeah, it will be great if you could share the code with us. Can I request for a copy of it? Many thanks! Are you able to upload it, or do you need my email address?

Best,
David
  #8  
Old 15-Dec-2005, 15:28
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Memory de-allocation during debugging


Well, here it is ...
But know it is not completed and there are some bugs ! The main problem right now is with the mrealloc function (it fails in the part where it calls the function to remove an item from the memory pool), as you could see for yourself .

MemoryHandling.h
CPP / C++ / C Code:

#include <stdlib.h>
#include <stdio.h>

#define HASH_SIZE	131

/*This type defines a memory allocation*/
typedef struct mem_alloc
{
	/*The address of the pointer to the memory block*/
	void		*address;
	/*Number of bytes held by this allocation*/
	unsigned	size;
} memory_allocation;

/*This type defines a memory allocation node to be stored in the allocation table*/
typedef struct mem_alloc_node
{
	memory_allocation		allocation;
	struct mem_alloc_node	*next;
	struct mem_alloc_node	*pre;
} memory_allocation_node;

typedef struct memory_pool_bucket
{
	memory_allocation_node *head;
	memory_allocation_node *tail;
} bucket;

/*The memory pool*/
typedef bucket memory_pool[HASH_SIZE];

void *mcalloc(size_t nobj,size_t size,memory_pool *my_memory_pool);
void *mmalloc(size_t size,memory_pool *my_memory_pool);
void *mrealloc(void *p,size_t size,memory_pool *my_memory_pool);
void mfree(void *p);

/*Search for an allocation in memory pool*/
memory_allocation_node *search_allocation(void *p,memory_pool *my_memory_pool);

/*The memory pool is implemented as a chained hash table 
(the chaining is done with doubly linked lists)
Init all memory pool buckets to 'NULL'.*/
void		init_memory_pool(memory_pool *my_memory_pool);
/*Free all allocations stored in the memory pool*/
void		free_memory_pool(memory_pool *my_memory_pool,FILE *destination); 
/*Calculate the sum of all allocations in memory pool. Return the result in bytes.*/
unsigned	get_memory_pool_size(memory_pool *my_memory_pool);
/*Print memory pool*/
void		print_memory_pool(memory_pool *my_memory_pool,FILE *destination);
/*Add allocation to memory pool*/
void		add_allocation_to_memory_pool(memory_allocation_node *new_allocation
										  ,memory_pool *my_memory_pool);
/*Remove allocation from memory pool*/
void		remove_allocation_from_memory_pool(memory_allocation_node *the_allocation
											   ,memory_pool *my_memory_pool);

/*Robert Jenkins' 32 bit Mix Function with the following modification :
The returend value is the key calculated by the original function, modulo HASH_SIZE*/
unsigned int inthash(unsigned int key);


MemoryHandling.c

CPP / C++ / C Code:

#include "memory_handling.h"

int main()
{
	int *p,*j,*k;
	memory_pool my_memory_pool;
	j = malloc(sizeof(int));
	init_memory_pool(&my_memory_pool);

	p = mmalloc(20,&my_memory_pool);
	j = mrealloc(p,25,&my_memory_pool);
	print_memory_pool(&my_memory_pool,stdout);
}

void *mmalloc(size_t size,memory_pool *my_memory_pool)
{
	void *result = NULL;
	/*Allocate memory*/
	result = malloc(size);
	if (result == NULL)
	{
		fprintf(stderr,"<malloc> Couldn't allocate memory.\n");
	}
	else
	{
		memory_allocation_node *new_allocation;
		new_allocation = malloc(sizeof(memory_allocation_node));
		/*If we can't allocate memory for memory pool allocation, 
		there will be a possible memory leak ! therefor, this is equal to 
		general memory allocation failure.*/
		if (new_allocation == NULL)
		{
			void *dummy = result;
			fprintf(stderr,"<malloc> Couldn't allocate memory.\n");
			free(dummy);
			result = NULL;
		}
		else
		{
			/*Memory allocation success. Add allocation to memory pool*/
			new_allocation->allocation.address = result;
			new_allocation->allocation.size = size;
			new_allocation->pre = NULL;
			add_allocation_to_memory_pool(new_allocation,my_memory_pool);
		}
	}
	return (result);
}

void *mcalloc(size_t nobj,size_t size,memory_pool *my_memory_pool)
{
	void *result = NULL;
	/*Allocate memory*/
	result = calloc(nobj,size);
	if (result == NULL)
	{
		fprintf(stderr,"<calloc> Couldn't allocate memory.\n");
	}
	else
	{
		memory_allocation_node *new_allocation;
		new_allocation = malloc(sizeof(memory_allocation_node));
		/*If we can't allocate memory for memory pool allocation, 
		there will be a possible memory leak ! therefor, this is equal to 
		general memory allocation failure.*/
		if (new_allocation == NULL)
		{
			void *dummy = result;
			fprintf(stderr,"<malloc> Couldn't allocate memory.\n");
			free(dummy);
			result = NULL;
		}
		else
		{
			/*Memory allocation success. Add allocation to memory pool*/
			new_allocation->allocation.address = result;
			new_allocation->allocation.size = nobj * size;
			new_allocation->pre = NULL;
			add_allocation_to_memory_pool(new_allocation,my_memory_pool);
		}
	}
	return (result);
}

void *mrealloc(void *p,size_t size,memory_pool *my_memory_pool)
{
	void *result;
	/*Allocate memory*/
	result = realloc(p,size);
	if (result == NULL)
	{
		fprintf(stderr,"<realloc> Couldn't (re)allocate memory.\n");
	}
	else
	{
		/*Create the new allocation*/
		memory_allocation_node *new_allocation;
		new_allocation = malloc(sizeof(memory_allocation_node));
		if (new_allocation == NULL)
		{
			fprintf(stderr,"<malloc> Couldn't allocate memory.\n");
			result = NULL;
		}
		else
		{
			memory_allocation_node *old_allocation;
			new_allocation->allocation.address = result;
			new_allocation->allocation.size = size;
			new_allocation->pre = NULL;
			/*Remove old allocation from memory pool*/
			old_allocation = search_allocation(p,my_memory_pool);
			if (old_allocation == NULL)
			{
				result = NULL;
			}
			else
			{
				remove_allocation_from_memory_pool(old_allocation,my_memory_pool);
				add_allocation_to_memory_pool(new_allocation,my_memory_pool);
			}
			
		}
	}
	return (result);
}

void		init_memory_pool(memory_pool *my_memory_pool)
{
	unsigned i;

	for (i = 0;i < HASH_SIZE;i++)
	{
		(*my_memory_pool)[i].head = NULL;
		(*my_memory_pool)[i].tail = NULL;
	}
}

void		free_memory_pool(memory_pool *my_memory_pool,FILE *destination)
{
	memory_allocation_node *iterator,*dummy;
	unsigned current_bucket;
	unsigned allocations_counter = 0;
	unsigned bytes_counter = 0;

	for (current_bucket = 0;current_bucket < HASH_SIZE;current_bucket++)
	{
		for (iterator = (*my_memory_pool)[current_bucket].head;iterator != NULL;iterator = dummy)
		{
			allocations_counter++;
			bytes_counter += iterator->allocation.size;
			
			dummy = iterator->next;
			fprintf(destination,"Releasing %-8u bytes starting from address 0x%X,memory pool bucket #%u\n",iterator->allocation.size,
					iterator->allocation.address,current_bucket);
			free(iterator);
		}
	}
	fprintf(destination,"Total allocations released : %u\n",allocations_counter);
	fprintf(destination,"Total bytes released : %u\n",bytes_counter);
	init_memory_pool(my_memory_pool);
}

unsigned	get_memory_pool_size(memory_pool *my_memory_pool)
{
	unsigned result = 0;
	unsigned i;
	memory_allocation_node *iterator;
	
	for (i = 0;i < HASH_SIZE; i++)
	{
		iterator = (*my_memory_pool)[i].head;
		while (iterator != NULL)
		{
			result += iterator->allocation.size;
			iterator = iterator->next;
		}
	}

	return (result);
}

void		print_memory_pool(memory_pool *my_memory_pool,FILE *destination)
{
	memory_allocation_node *iterator;
	unsigned current_bucket;
	for (current_bucket = 0;current_bucket < HASH_SIZE;current_bucket++)
	{
		fprintf(destination,"Bucket #%u\n",current_bucket);
		iterator = (*my_memory_pool)[current_bucket].head;
		while (iterator != NULL)
		{
			fprintf(destination,"Address :0x%.8X\tAllocation size :%u \tbytes\n",iterator->allocation.address,iterator->allocation.size);
			iterator = iterator->next;
		}
	}
}

void		add_allocation_to_memory_pool(memory_allocation_node *new_allocation
										  ,memory_pool *my_memory_pool)
{
	unsigned memory_pool_bucket;
	memory_pool_bucket = inthash((unsigned int)new_allocation->allocation.address);
	if ((*my_memory_pool)[memory_pool_bucket].head == NULL)
	{
		/*Insert memory allocation as the first allocation in the bucket*/
		new_allocation->next = NULL;
		(*my_memory_pool)[memory_pool_bucket].head = new_allocation;
		(*my_memory_pool)[memory_pool_bucket].tail = new_allocation;
	}
	else
	{
		/*Insert memory allocation at the head of the bucket*/
		new_allocation->next = (*my_memory_pool)[memory_pool_bucket].head;
		(*my_memory_pool)[memory_pool_bucket].head->pre = new_allocation;
		(*my_memory_pool)[memory_pool_bucket].head = new_allocation;
	}
}

void		remove_allocation_from_memory_pool(memory_allocation_node *the_allocation
											   ,memory_pool *my_memory_pool)
{
	bucket *the_list;
	unsigned bucket_index;
	bucket_index = inthash((unsigned int)the_allocation->allocation.address) % HASH_SIZE;
	the_list = my_memory_pool[bucket_index];
	if (the_allocation->next == NULL && the_allocation->pre == NULL)
	{
		the_list->head = NULL;
	}
	else if (the_allocation->next == NULL && the_allocation->pre != NULL)
	{
		the_allocation->pre->next = NULL;	
	}
	else if (the_allocation->next != NULL && the_allocation->pre == NULL)
	{
		the_allocation->next->pre = NULL;
		the_list->head = the_allocation->next;
	}
	else if (the_allocation->next != NULL && the_allocation->pre != NULL)
	{
		the_allocation->pre->next = the_allocation->next;
		the_allocation->next->pre = the_allocation->pre;
	}
}

memory_allocation_node *search_allocation(void *p,memory_pool *my_memory_pool)
{
	memory_allocation_node *result = NULL;
	memory_allocation_node *iterator;
	unsigned current_bucket;
	
	for (current_bucket = 0;(current_bucket < HASH_SIZE) && (result == NULL);current_bucket++)
	{
			iterator = (*my_memory_pool)[current_bucket].head;
			while ((iterator != NULL) && (result == NULL))
			{
				if (iterator->allocation.address == p)
				{
					result = iterator;
				}
				iterator = iterator->next;
			}
	}
	return (result);
}

unsigned int inthash(unsigned int key)
{
  key += (key << 12);
  key ^= (key >> 22);
  key += (key << 4);
  key ^= (key >> 9);
  key += (key << 10);
  key ^= (key >> 2);
  key += (key << 7);
  key ^= (key >> 12);
  return (key % HASH_SIZE);
}


The memory "m"reallocation problem is something I'm hoping to fix as soon as I could spare time.

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #9  
Old 15-Dec-2005, 17:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Memory de-allocation during debugging


Quote:
Originally Posted by kobi_hikri
The main problem right now is with the mrealloc function

I don't know if there are other problems, but:

CPP / C++ / C Code:
void *mrealloc(void *p,size_t size,memory_pool *my_memory_pool)
{
.
.
.
  result = realloc(p,size); /* <<< Note 1 */
.
.
.
    new_allocation = malloc(sizeof(memory_allocation_node)); /* <<< Note 2 */
.
.
.
      old_allocation = search_allocation(p,my_memory_pool);
        remove_allocation_from_memory_pool(old_allocation,my_memory_pool); /* <<< Note 3 */
.
.
.
}


Note 1: the pointer p might no longer point to memory that you own.
Note 2: this malloc could give the same memory region that used to be p.
Note 3: now you are screwed (since you will be de-referencing pointers in a block of memory that you no longer own, and you don't know what their values are).

I put some printfs after malloc() and realloc() calls and discovered that the condition in Note 2 occurred with your example program, so, well ..., see Note 3. Other compilers and/or other data sizes could give different results that appear to work under some conditions, but this is a fatal bug.

(gcc Linux)

Regards,

Dave
  #10  
Old 15-Dec-2005, 17:41
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Memory de-allocation during debugging


Quote:
Originally Posted by davekw7x
Note 1: the pointer p might no longer point to memory that you own.

Yeah, This is what I understood from another question I asked here on the forum (Another thread, if you remeber - I asked something about realloc and you answered me). Didn't fix that yet ... But will fix it soon (hopefully).

Kobi.
__________________
It's actually a one time thing (it just happens alot).
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory allocation issue kobi_hikri C++ Forum 6 27-Oct-2005 12:16
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
3D array dynamic memory allocation cjwatchdog C Programming Language 3 20-Feb-2004 15:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.