malloc을 이용한 간단한 리스트 구현에서의 메모리 릭 발생 문제에 대해 여쭤봅니다.

jchaney의 이미지

안녕하세요.

어제 올렸던 질문과 연장선에 있는 문제인데,

어제 올렸던 코드가 잘 되지 않아서, 제가 간단한 리스트를 구현하여 테스트를 해 보았습니다.

그런데, 이 테스트 코드에서도 이전과 같은 문제점이 발생되어서 질문을 드리게 되었습니다.

간단히 생성을 하고, 화면에 그 값을 출력해주고, 메모리 해제를 해주는 코드인데,

메모리 해제부분에서 제가 놓친부분이 있어서 메모리 릭이 발생하고 있습니다.

메모리 릭에 대한 테스트는 valgrind를 이용하여 테스트를 하였습니다.

소스 코드를 보시고, 문제가 될 수 있는 부분을 집어주시면 정말 감사하겠습니다.

그럼 좋은 하루 보내시기 바랍니다.

----------------------------------------------------------------------------------
test.cpp

#include
#include
#include

class Element
{
public :
Element()
{
type = 0;
data = NULL;
};
~Element()
{
printf("~Element\n");
type = 0;
if(data != NULL)
{
free(data);
data = NULL;
}
};
void init(int t, char *d)
{
//printf("t : %d, d : %s\n", t, d);
type = t;
data = (char *) malloc(strlen(d));
memcpy(data, d, strlen(d));
};

int type;
char *data;
};

void testMalloc()
{
int maxSize = 10;
int *isUsed;
Element **eList;

// init
isUsed = (int *) malloc(sizeof(int) * maxSize);
eList = (Element **) malloc(sizeof(Element *) * maxSize);

char buf[100];
for(int i = 0; i < maxSize; i++)
{
isUsed[i] = i;
sprintf(buf, "data%d", i);
eList[i] = new Element;
eList[i]->init(i, buf);
}

// printout
for(int i = 0; i < maxSize; i++)
{
printf("isUsed[%d] : %d\n", i, isUsed[i]);
printf("eList[%d]->type : %d, data : %s\n",
i, eList[i]->type, eList[i]->data);
}

// delete
for(int i = 0; i < maxSize; i++)
{
if(eList[i] != NULL)
{
delete(eList[i]);
eList[i] = NULL;
}
}

if(isUsed != NULL)
{
free(isUsed);
isUsed = NULL;
}
if(eList != NULL)
{
free(eList);
eList = NULL;
}
}
int main()
{
testMalloc();

return 0;
}

jchaney의 이미지

위의 코드를 계속 디버깅해본 결과, 메모리 릭은 아니었고, 잘못된 메모리 참조(Invalid read)였었는데

문제가 되던 부분은 Element클래스에서 data 스트링을 프린트 아웃하는 경우에,

종결 문자가 없어서 발생된 문제였던것 같습니다.

정확하게 body크기만큼을 케릭터 별로 프린트 아웃을 해주거나,

사이즈를 +1만큼해주어서 memset(body, 0x00, strlen(b) + 1)을 한 후에 memcpy()를 하면

valgrind에서 에러가 발생하지 않았습니다.

혹시 저와 같은 경우를 당하신 분들에게 참고가 되었으면 합니다. ㅋ

jchaney

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.