LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   memory allocation (https://www.linuxquestions.org/questions/programming-9/memory-allocation-376342/)

gecoool 10-24-2005 07:55 AM

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.

dmail 10-24-2005 08:12 AM

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.

gecoool 10-24-2005 09:47 AM

thank you dmail ;-) i will try this !


All times are GMT -5. The time now is 09:51 AM.