c++ 동적할당된 메모리 자동 해제

auditory의 이미지

new 로 alloc된 메모리를 특정시점에 자동으로 모두 delete시킬 수 있는 방법이 있을까요?

main()
{
 func1();
 func2();
}

와 같은 코드에서 func1 안에서 new로 메모리를 많이 alloc시킨후에 free하지 않고 종료합니다.

이때 func2를 시작하기 전에 이전에 new로 할당된 메모리를 모두 자동으로 해제하려고 합니다.

가장 간단한 방법이 어떤게 있을까요??

func1의 소스도 모두 가지고 있습니다만,

직접 고치기에는 너무 복잡한 상황입니다.

winner의 이미지

사용하시는 compiler의 문서를 자세히 읽는 것은 덤입니다.

kaeri17의 이미지

전역 new 오퍼레이터를 오버로딩 해야 할듯 싶네요...

auditory의 이미지

현재까지 고려중인 방법으로는

다른분들께서 말씀하신대로

1. new를 overloading해서 할당된 주소와 크기를 모두 저장했다가, 나중에 해제하는 방법
2. 미리 메모리풀을 만들고, new를 overloading해서 메모리 풀안에서 할당하도록 한후,
함수 종료후 메모리풀을 해제하는 방법

을 고려하고 있습니다..
1번보다는 2번이 간단할것 같은데요..
전역 new를 이런식으로 overloading하는 것이 가능할까요?

winner의 이미지

어떤 형태로도 말이죠. 내부에서 hacking을 하면 안 될 것은 없겠죠. 하지만 까다롭고, 문제없이 만들려면 여러가지로 고민을 해야 합니다.
더 큰 문제는 잘못된 설계가 유지되고, 더 악화될 가능성도 있습니다.
단기적으로 memory 누수를 확인한다던가 그런 것이 아니라면 추천하고 싶지는 않네요.

neogeo의 이미지

smart pointer 를 사용해보세요. func1 이 종료되는 시점에 정리가 되도록 할 수 있습니다.

Neogeo - Future is Now.

philnet의 이미지

operator new/delete overloading을 thread safe 하게, dead-lock 없이 작성하려면 설계와 검증에 상당한 공을 들여야 할 겁니다.

func1 에서 동적 할당되는 부분들을, boost::shared_ptr와 같은 검증(?)된 smart pointer로 교체하는 것이 여러 모로 안전(?)합니다.

unipro의 이미지

프로그램을 나눈 다음에 둘 사이에 통신 방법을 고려해보는 것은 어떨까요?
파이프, 공유메모리 등등...
자원 해제는 OS에 맡겨놓구요...

내 블로그: http://unipro.tistory.com

댓글 달기

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