LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2005, 08:55 AM   #1
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Rep: Reputation: 15
memory allocation


Hello everyone ;-)

I wrote a small app in c++, (an html parser, based on tidylib). My primary problem is an ugly memory leak. I tried valgrind but it shows fairly large amounts of memory as possibly lost or stiil reachable. Finally I wrote a small function that parses /proc/<my_app_pid>/status from where I get the VmRSS memory (also returned by top -p <my_app_pid>). I see that the memory gets allways allocated in blocks of 4K (even if at any given time my code doesn't ask that much memory at once ...), but never gets deallocated. Even if am using local variables, (i.e. stl maps, strings wich should be created and destroyed, every time the parser function is called) the memory increases continuosly. Please help.
 
Old 10-24-2005, 09:12 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
maybe this will help you track down the leaks.

Code:
/*=============================================================================
memory_check.h
File description:
checks for memory leaks and writes to file any memory which isnt released via
the delete call

remarks:
this is not all my code it is derived from a post by Dion Picco on flipcode
http://www.flipcode.com/articles/art...oryleaks.shtml
many thanks

author:
=============================================================================*/

#ifndef _MEMORY_CHECK_H_
#define _MEMORY_CHECK_H_

#ifdef _DEBUG //just to make sure its only in debug versions and not release

///#include "stdafx.h"
#include <stdlib.h>
#include <malloc.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>


using namespace std;

__inline string cstring2string(char* c_string)
{
	string message;

	for(int i=0; i<=(int)strlen(c_string); i++)
		message = message  + c_string[i];

	return message;
}

typedef struct 
{
	void* address;
	unsigned int size;
	string file;
	int line;
} memory_info;

class Memory
{
public:
	static Memory* get_manager(); 
	~Memory();
	void add(unsigned int size, char* file, int line, void* address);
	void remove(void* address);
	void save_to_file();
private:
	ofstream out;
	Memory();
	static Memory* m_manager;
	vector<memory_info> ptr_vector;
	vector<memory_info> ::iterator ptr_iter;
};

Memory* Memory::m_manager = 0;

Memory::Memory()
{
	;
}

Memory::~Memory()
{
	delete m_manager;
}

Memory* Memory::get_manager()
{
	if (m_manager == 0)
		m_manager = new(Memory);

	return m_manager;
}

void Memory::add(unsigned int size, char* file, int line, void* address)
{
	memory_info info;
	info.size = size;
	info.file = cstring2string(file);
	info.line = line;
	info.address = address;
	ptr_vector.push_back(info);
}

void Memory::remove(void* address)
{
	for(ptr_iter = ptr_vector.begin(); ptr_iter != ptr_vector.end(); ++ptr_iter)
	{
		if(ptr_iter->address == address)
		{

			ptr_vector.erase(ptr_iter);
			break;
		}
	}
}
void Memory::save_to_file()
{
	char file_name[]="memory_leaks.txt";

	out.open( file_name, ios::out /*| ios::app*/ );

	if(!out.is_open())
	{
		cout <<"ERROR\ncould not open file " <<file_name <<endl;
	}
	else 
	{
		for(ptr_iter = ptr_vector.begin(); ptr_iter != ptr_vector.end(); ++ptr_iter)
		{
			out <<"address:" <<ptr_iter->address <<" "
				<<"size:" <<ptr_iter->size <<" "
				<<"file:" <<ptr_iter->file <<" "
				<<"line:" <<ptr_iter->line <<" " <<"\n";
		}
		out.close();
	}

}


void*  operator new(unsigned int size,char* file, int line)
{
	void *ptr = (void *)malloc(size);
	if(ptr == 0)
	{
		error("memory could not be allocated!");
	}
		Memory::get_manager()->add(size,file,line,ptr);
	return ptr;
}

void operator delete(void* ptr)
{
	Memory::get_manager()->remove(ptr);
	//cant seem to use the destructot for the Memory class
	//or check if the pointer is the memory ptr so I will just have to write it everytime ;(
	//slow but what can I do??????????????
	Memory::get_manager()->save_to_file();
	free(ptr);
}

//some macro magic to make the new call give its line and file
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW


#endif  //_DEBUG
#endif //_MEMORY_CHECK_H_

edit--
the error func referenced in this file is:
Code:
#define error(X) std::cout <<"Error: " <<X <<std::endl <<"\tfile: "<<__FILE__ <<std::endl << "\tline: "<<__LINE__ <<std::endl;

and this file doesnt check that a valid pointer is returned, its just for debug purposes.

Last edited by dmail; 10-24-2005 at 09:20 AM.
 
Old 10-24-2005, 10:47 AM   #3
gecoool
Member
 
Registered: Feb 2005
Location: Romania
Distribution: Fedora 2
Posts: 38

Original Poster
Rep: Reputation: 15
thank you dmail ;-) i will try this !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory allocation esael Linux - General 7 01-12-2008 01:12 PM
Help - memory allocation in C zaichik Programming 3 09-04-2005 11:16 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 10:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 10:22 PM
memory allocation. raven Programming 5 09-08-2002 02:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration