Valgrind - 함수 밖에서 메모리 해제 할 경우

gkaudrla45의 이미지

안녕하세요. 가입하고 처음 질문글 올려봅니다 :)

제가 만든 프로그램이 메모리 누수가 나는 것 같아서 valgrind를 이용해서 메모리 누수를 잡으려고 했는데요. 분명히 메모리 해제를 해준 것 같은 부분에서도 valgrind는 메모리 누수를 출력합니다.

그래서 그 부분을 보니 함수 안에서 메모리 동적할당을 하고 그 함수 밖에서 메모리를 해제해주고 있었습니다.

위와 같은 상황을 간단한 코드로 만들어서 테스트 해보았는데요.

#include <iostream>
 
void test(int *var) {
        var = new int;
}
 
int main(void) {
        int *var;
 
        test(var);
 
        delete var;
 
        return 0;
}

보시다시피 test함수내에서 동적할당을 받고 함수 밖에서 메모리해제를 하고 있습니다.

이 코드를 valgrind --leak-check=yes ./test 명령어를 입력해서 메모리 누수 체크를 해보았고 결과는 아래와 같았습니다.

==27625== Memcheck, a memory error detector
==27625== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==27625== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==27625== Command: ./main
==27625==
==27625== Conditional jump or move depends on uninitialised value(s)
==27625== at 0x4A05F89: operator delete(void*) (vg_replace_malloc.c:480)
==27625== by 0x4006F7: main (in /home/gkaudrla45/main)
==27625==
==27625==
==27625== HEAP SUMMARY:
==27625== in use at exit: 4 bytes in 1 blocks
==27625== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==27625==
==27625== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==27625== at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==27625== by 0x4006D1: test(int*) (in /home/gkaudrla45/main)
==27625== by 0x4006EB: main (in /home/gkaudrla45/main)
==27625==
==27625== LEAK SUMMARY:
==27625== definitely lost: 4 bytes in 1 blocks
==27625== indirectly lost: 0 bytes in 0 blocks
==27625== possibly lost: 0 bytes in 0 blocks
==27625== still reachable: 0 bytes in 0 blocks
==27625== suppressed: 0 bytes in 0 blocks
==27625==
==27625== For counts of detected and suppressed errors, rerun with: -v
==27625== Use --track-origins=yes to see where uninitialised values come from
==27625== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

네.. 메모리 누수가 났다고 말하고 있습니다.

이런 경우는 valgrind로는 못잡는건가요? 아니면 옵션같은 걸 추가로 줘야하는건지(옵션들을 살펴보긴 했는데 뭘하는 옵션들인지 잘은 모르겠더라구요..) 궁금합니다.

감사합니다.

pchero의 이미지

메모리 누수가 맞습니다.

기존소스에서 delete var 과 test 에서 메모리를 할당한 곳은 서로 메모리 주소가 다릅니다.
아래 코드와 비교해보시기 바랍니다.

#include <iostream>
 
void test(int **var) {
        *var = new int;
}
 
int main(void) {
        int *var;
 
        test(&var);
 
        delete var;
 
        return 0;
}

---------------------------------
제일 왼쪽이 저입니다 :)

gkaudrla45의 이미지

참 민망한 코드를 올렸네요 -_-;; 후다닥 만들다가 ..

애꿎은 valgrind 탓만 했군요ㅋㅋ

감사합니다!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.