|
-
March 16th, 2012, 02:57 AM
#1
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
-
March 16th, 2012, 03:33 AM
#2
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.
-
March 16th, 2012, 04:40 AM
#3
Re: Memory Leak using string
 Originally Posted by himitsujanai
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|