CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    10

    Memory Leak using string

    Hi guys its been a while i just wanna ask if you have any idea why this code leaking. The below code is just a sample representation on how it works in one of the parts of my actual program that causes leaks.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    
    #ifdef _DEBUG
    	#define MEMLEAK_CHECK_ON
    #endif
    
    #ifdef MEMLEAK_CHECK_ON
    	#define CRTDBG_MAP_ALLOC
    	#include <stdlib.h>
    	#include <crtdbg.h>
    #endif
    
    using namespace std;
    
    struct sample
    {
    	string *name;
    	int xpos;
    	int ypos;
    };
    
    sample *a = NULL;
    sample *b = NULL;
    void init();
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	init();
    
    	a->name[0].clear();
    	a->name[1].clear();
    	delete a;
    
    #ifdef MEMLEAK_CHECK_ON
    	_CrtDumpMemoryLeaks();
    #endif
    	return 0;
    }
    void init()
    {
    	a = new sample();
    	a->name = new string[2];
    	a->name[0] ="Sample1";
    	a->name[1] = "Sample2";
    	a->xpos = 12;
    	a->ypos = 13;
    	size_t nLength = a->name->length();
    
    }
    Thanks in advance

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Memory Leak using string

    Even though you delete the struct the string isn't deleted. Remove the pointer to the string and use the string directly instead.
    You can also add a destructor to the struct but avoiding to allocating yourself is better.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory Leak using string

    Quote Originally Posted by himitsujanai View Post
    The below code is just a sample representation on how it works in one of the parts of my actual program that causes leaks.
    Then I suggest rewriting those other parts of your program that looks like what you posted. There is no need for all of this dynamic allocation, at least from what you posted.

    Why are you allocating an array of size 2? You know exactly how many elements there are in the array, so why not just declare an array of 2 strings? If however, you really want an array that is dynamically allocated, then you have std::vector. Then you have no memory leaks.

    Third, what is length() supposed to do? If it's to get the number of elements in an array, then C++ is not Java. There is no length() function for arrays.
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <vector>
    
    #ifdef _DEBUG
    	#define MEMLEAK_CHECK_ON
    #endif
    
    #ifdef MEMLEAK_CHECK_ON
    	#define CRTDBG_MAP_ALLOC
    	#include <stdlib.h>
    	#include <crtdbg.h>
    #endif
    
    using namespace std;
    
    struct sample
    {
    	vector<string> name;
    	int xpos;
    	int ypos;
    };
    
    sample a;
    sample b;
    
    void init();
    int _tmain(int argc, _TCHAR* argv[])
    {
    	a.name[0].clear();
    	a.name[1].clear();
    	return 0;
    }
    
    void init()
    {
            a.name.resize(2);
    	a.name[0] ="Sample1";
    	a.name[1] = "Sample2";
    	a.xpos = 12;
    	a.ypos = 13;
    	size_t nLength = a.name.size();
    }
    The code above has no memory leaks for the simple reason being that the user has not dynamically allocated any memory. All of those details are taken care of by the string and vector classes.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 16th, 2012 at 04:43 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured