memory leaks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: memory leaks

  1. #1
    Join Date
    Dec 2009
    Posts
    161

    memory leaks

    Hi,

    do you think the following code could lead to memory leaks?

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <windows.h>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    #include <cstdio>
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;
    
    char * getDateTime(void);
    const short numbuff = 5;
    const short buflen  = 30;
    
    typedef struct
    {
    	unsigned char * pData;
    	unsigned short bufferLength;
    	unsigned short bytesRecorded;
    	bool flag;
    } Buffer;
    
    int main()
    {
    	circular_buffer<Buffer*> cb(numbuff);
    	circular_buffer<Buffer*>::const_iterator it;
    
    	// fill buffer
    	for(int i = 0; i<numbuff; i++)
    	{
    		// set up buffer
    		Buffer *buff       = new Buffer;
    		ZeroMemory(buff, sizeof(Buffer));
    
    		buff->bufferLength  = buflen;
    		buff->bytesRecorded = buflen;
    		buff->flag          = true;
    		buff->pData         = new unsigned char[buflen];
    		buff->pData         = reinterpret_cast<unsigned char *>(getDateTime());
    
    		// push buffer
    		cb.push_back(buff);
    
    		Sleep(1000);
    	}
    
    	// show elements
    
    	for(int i = 0; i<(int)cb.size(); i++)
    	{
    		printf("%s\n", cb[i]->pData);
    	}
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    // getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
    char * getDateTime(void)
    {
    	time_t rawtime;
    	struct tm * timeinfo;
    	time(&rawtime);
    	timeinfo = gmtime(&rawtime);
    	char * buffer = new char[30];
    	strftime(buffer,30,"%a, %d %b %Y %X GMT",timeinfo);
    	return buffer;
    }
    I cannot delete "buff" since its scope is inside the first for loop...

  2. #2
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    Hi,

    do you think the following code could lead to memory leaks? it creates a
    circualr buffer made up of 5 element then it pushes 10 elements (overwriting
    the first 5) when overwriting , do you think the eldest pointed memory will
    be overwritten, or a new pointer in memory will be written?

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <windows.h>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    #include <cstdio>
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;
    
    char * getDateTime(void);
    const short numbuff = 5;
    const short buflen  = 30;
    
    typedef struct
    {
     unsigned char * pData;
     unsigned short bufferLength;
     unsigned short bytesRecorded;
     bool flag;
    } Buffer;
    
    int main()
    {
     circular_buffer<Buffer*> cb(numbuff);
     circular_buffer<Buffer*>::const_iterator it;
    
     printf("Push elements:\n");
     // fill buffer
     for(int i = 0; i<10; i++)
     {
      // set up buffer
      Buffer *buff       = new Buffer;
      ZeroMemory(buff, sizeof(Buffer));
    
      buff->bufferLength  = buflen;
      buff->bytesRecorded = buflen;
      buff->flag          = true;
      buff->pData         = new unsigned char[buflen];
      buff->pData         = reinterpret_cast<unsigned char *>(getDateTime());
    
      printf("%s\n", buff->pData);
    
      // push buffer
      cb.push_back(buff);
    
      Sleep(1000);
     }
    
     printf("\nShow elements:\n");
    
     // show elements
     for(int i = 0; i<static_cast<int>(cb.size()); i++)
     {
      printf("%s\n", cb[i]->pData);
     }
    
     system("pause");
     return EXIT_SUCCESS;
    }
    
    // getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
    char * getDateTime(void)
    {
     time_t rawtime;
     struct tm * timeinfo;
     time(&rawtime);
     timeinfo = gmtime(&rawtime);
     char * buffer = new char[30];
     strftime(buffer,30,"%a, %d %b %Y %X GMT",timeinfo);
     return buffer;
    }
    // thanks

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: memory leaks

    buff->pData = new unsigned char[buflen];
    buff->pData = reinterpret_cast<unsigned char *>(getDateTime());
    This leaks. pData is overwritten the 2nd time so the memory you created the 1st time is gone.
    Also... who frees the memory that 'getDateTime' has created.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,546

    Re: memory leaks

    All that you newed you *must* delete. Period.
    If you don't delete what you have newed then you produce memory leaks.
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    Quote Originally Posted by Skizmo View Post
    This leaks. pData is overwritten the 2nd time so the memory you created the 1st time is gone.
    Also... who frees the memory that 'getDateTime' has created.
    by the way, how can I change getDateTime() to return char[30] ?? I mean the return type...

    thanks

  6. #6
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    Quote Originally Posted by VictorN View Post
    All that you newed you *must* delete. Period.
    If you don't delete what you have newed then you produce memory leaks.
    Ok. As I was suggested in this very forum...I am going to use vector to collect chars...is that ok? and no pointers anymore...

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,546

    Re: memory leaks

    Quote Originally Posted by THEARTOFWEB View Post
    by the way, how can I change getDateTime() to return char[30] ?? I mean the return type...

    thanks
    For example:
    Code:
     void getDateTime( char *buffer)
    {
     time_t rawtime;
     struct tm * timeinfo;
     time(&rawtime);
     timeinfo = gmtime(&rawtime);
    
     strftime(buffer,30,"%a, %d %b %Y %X GMT",timeinfo);
    }
    Now you can:
    Code:
    char buffer[30];
    getDateTime(buffer);
    Victor Nijegorodov

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,546

    Re: memory leaks

    Quote Originally Posted by THEARTOFWEB View Post
    ...I am going to use vector to collect chars...is that ok? and no pointers anymore...
    Yes, it is definetly a very good idea!
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    For instance...do you think the following could be better approach?

    Code:
    struct Buffer
    {
    public:
    	vector<unsigned char> vChar;
    	unsigned int bufferLength;
    	unsigned int bytesRecorded;
    	Buffer() : bytesRecorded(0), bufferLength(0), vChar(NULL) { };
    };
    
    int main()
    {
    	circular_buffer<Buffer> cb(numbuff);
    	circular_buffer<Buffer>::const_iterator it;
    
    	(...)
    }
    thanks

  10. #10
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    anyway, I have a problem with assign a char to a vector:

    Code:
    void getDateTime(char * szTime);
    const int numbuff = 5;
    const int buflen  = 30;
    
    struct Buffer
    {
    public:
    	vector<char> vChar;
    	unsigned int bufferLength;
    	unsigned int bytesRecorded;
    	Buffer() : bytesRecorded(0), bufferLength(0), vChar(NULL) { };
    };
    
    int main()
    {
    	circular_buffer<Buffer> cb(numbuff);
    	circular_buffer<Buffer>::const_iterator it;
    
    	for(int i = 0; i<10; i++)
    	{
    		// Get time
    		char szTime[30]; getDateTime(szTime);
    
    		// Init Buff
    		Buffer buff;
    		ZeroMemory(&buff, sizeof(Buffer));
    
    		buff.vChar.resize(buflen);
    		buff.vChar         = szTime;
    		buff.bufferLength  = buflen;
    		buff.bytesRecorded = buflen;
    
    		printf("%s\n", buff.vChar);
    	}
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    // getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
    void getDateTime(char * szTime)
    {
    	time_t rawtime = time(NULL);
    	struct tm timeinfo;
    	gmtime_s(&timeinfo, &rawtime);
    	strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
    }
    the code fails here:

    Code:
    buff.vChar = szTime;
    why?

    thanks

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,546

    Re: memory leaks

    Because szTime is not a char
    Victor Nijegorodov

  12. #12
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    do you mean szTime is several chars ??

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,546

    Re: memory leaks

    No, it is a pointer to a NULL-terminated string (character array)
    Victor Nijegorodov

  14. #14
    Join Date
    Dec 2009
    Posts
    161

    Re: memory leaks

    well, I thought this was a stand-alone string (chars array) not a pointer:

    Code:
    char szTime[30]; getDateTime(szTime);
    I tried using memcpy but it's not working...

  15. #15
    Join Date
    Apr 1999
    Posts
    27,444

    Re: memory leaks

    Quote Originally Posted by THEARTOFWEB View Post
    well, I thought this was a stand-alone string (chars array) not a pointer:

    Code:
    char szTime[30]; getDateTime(szTime);
    I tried using memcpy but it's not working...
    First, change the function getDateTime() to return a string, not a char*.
    Code:
    // getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
    std::string getDateTime(void)
    {
       time_t rawtime;
       struct tm * timeinfo;
       time(&rawtime);
       timeinfo = gmtime(&rawtime);
       char buffer[30];
       strftime(buffer,30,"&#37;a, %d %b %Y %X GMT",timeinfo);
       return buffer;
    }
    There really isn't a reason to resort to char* and new[] to create strings in a C++ application.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center