메모리 사용법 2 :: 2007/08/07 20:45



신입 개발자를 위한
메모리 사용법 2.
신영진 pop@jiniya.net http://www.jiniya.net

6월호 기사에서 우리는 함수로 넘어오는 인자와 함수의 리턴 값에 사용되는 메모리의 종류와 사용 방법에 대해서 배웠다. 하지만 실제 프로그램을 작성할 때에는 인자와 리턴 값 외에도 신경 쓸 부분이 많다. 이번 시간에는 Windows 메시지에 사용된 복잡한 특성의 메모리와 스마트 포인터를 사용해서 메모리 릭을 방지하는 방법에 대해서 살펴본다.

복잡한 특성을 가진 메모리
LVN_GETDISPINFO는 리스트 뷰 컨트롤에서 추가한 아이템의 내용을 콜백으로 설정하도록 했을 때 날라오는 메시지다. 이 메시지의 파라미터로는 LVITEM이 넘어온다. 대부분의 초급 Visual C++ 책에서는 이 메시지 핸들러에 다음과 같은 코드를 포함하고 있다.
아주 단순한 코드다. 그런 만큼 책에는 저 코드에 대한 어떠한 설명도 없다. 하지만 우리는 저러한 코드를 보면 “pszText에 대한 할당과 해제를 책임지는 것은 누구일까?”라는 질문을 꼭 던져야 한다.

pszText는 꽤나 복잡한 성격의 포인터다. 복사할 문자열의 길이가 cchTextMax보다 작다면 위와 같이 문자열 복사를 하면 된다. 만약 복사할 문자열이 더 길다면 pszText가 가리키는 포인터를 아래와 같이 바꾸어 주면 된다. 이 때 한 가지 주의해야 할 점은 pszText가 가리키고 있는 메모리가 적어도 두 번의 LVN_GETDISPINFO 메시지를 더 받은 다음 파괴되어야 한다는 점이다.
위와 같이 복잡한 특성은 LVN_GETDISPINFO를 설계한 사람이 아니라면 알 수 없기 때문에 API를 만든 사람은 친절하게 매뉴얼에(MSDN) 설명을 적어 놓았다. 따라서 위와 같이 이상하게 보이는 메모리가 있다면 반드시 매뉴얼을 통해서 그 메모리를 다루는 방법에 대해서 숙지한 다음 사용하도록 해야 한다.

스마트 포인터
스마트 포인터는 RAII (Resource acquisition is initialization)의 개념을 가장 잘 활용한 예 중의 하나다. RAII란 생성자에서 자원을 획득하고, 소멸자에서 자원을 자동적으로 파괴해야 한다는 원칙이다. <리스트 1>을 살펴보자. 할당한 메모리 ptr이, some_condition인 경우는 해제되지 않고 리턴 하기 때문에 릭이 생긴다. 바보 같은 코드처럼 보이지만 할당된 자원이 많고, 함수가 복잡해질수록 이러한 일은 더욱 쉽게 일어난다. 이러한 문제점을 스마트 포인터를 사용해서 변경한 코드가 <리스트 2>다. <리스트 2>의 char_ptr은 동작원리를 보여주기 위해서 간단하게 제작된 것이다. 실제 스마트 포인터의 구현은 훨씬 더 복잡하다. 하지만 걱정할 것 없다. 표준 라이브러이의 auto_ptr이나 boost 라이브러리의 shared_ptr과 같이 잘 구현된 스마트 포인터 클래스가 많이 있기 때문이다.

리스트 1 스마트 포인터를 사용하지 않은 코드
리스트 2 스마트 포인터를 사용한 코드


스폰서
글타래

  • 2주간 인기 글
  • 2주간 인기글이 없습니다.
Trackback Address :: http://jiniya.net/tt/trackback/554
  • Gravatar Image.
    snaiper | 2007/08/07 20:55 | PERMALINK | EDIT/DEL | REPLY

    글은 괜찮은데...
    RIIA (Resource initialization is acquisition)가 틀렸네...
    RAII (Resource Acquisition Is Initialization)가 맞는 표현이야

  • Gravatar Image.
    clip in store | 2020/07/23 04:43 | PERMALINK | EDIT/DEL | REPLY

    I think this is an informative post and it is very useful and knowledgeable. therefore. I would like to thank you for the efforts you have made in writing this article.

  • I high appreciate this post. It’s hard to find the good from the bad sometimes. but I think you’ve nailed it! would you mind updating your blog with more information?

  • Gravatar Image.
    image editing asia | 2020/07/30 14:58 | PERMALINK | EDIT/DEL | REPLY

    Positive site. where did u come up with the information on this posting? I'm pleased I discovered it though. ill be checking back soon to find out what additional posts you include.

  • This is highly informatics. crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things.

  • Gravatar Image.
    pg slot | 2020/07/30 17:27 | PERMALINK | EDIT/DEL | REPLY

    Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.

  • Gravatar Image.
    Dry Needling | 2020/08/09 18:54 | PERMALINK | EDIT/DEL | REPLY

    Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day!

  • Gravatar Image.
    israeli tv | 2020/08/10 05:00 | PERMALINK | EDIT/DEL | REPLY

    If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you.

  • Gravatar Image.
    immgration schengen | 2020/08/29 15:17 | PERMALINK | EDIT/DEL | REPLY

    Im no expert. but I believe you just made an excellent point. You certainly fully understand what youre speaking about. and I can truly get behind that.

  • Gravatar Image.
    yogametric | 2020/08/29 16:52 | PERMALINK | EDIT/DEL | REPLY

    The information you have posted is very useful. The sites you have referred was good. Thanks for sharing...

  • Gravatar Image.
    catalogue maroc | 2020/09/05 18:54 | PERMALINK | EDIT/DEL | REPLY

    Nice post and i found that the way of writing to unique and satisfying, I can find helpful information specially the middle part I, Thank you and keep posting.

  • Gravatar Image.
    sharp xe a177 | 2020/09/09 20:52 | PERMALINK | EDIT/DEL | REPLY

    I think this is an informative post and it is very useful and knowledgeable. therefore. I would like to thank you for the efforts you have made in writing this article.

  • Gravatar Image.
    winalist.nl | 2020/09/13 16:26 | PERMALINK | EDIT/DEL | REPLY

    These are some great tools that i definitely use for SEO work. This is a great list to use in the future..

  • Gravatar Image.
    immigration lawyer | 2020/09/28 19:47 | PERMALINK | EDIT/DEL | REPLY

    This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.!

  • Gravatar Image.
    level lawn | 2020/10/03 18:15 | PERMALINK | EDIT/DEL | REPLY

    I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..

  • Gravatar Image.
    tra vy tea | 2020/10/04 21:48 | PERMALINK | EDIT/DEL | REPLY

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..

  • Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!..

  • Gravatar Image.
    maid service | 2020/10/07 21:51 | PERMALINK | EDIT/DEL | REPLY

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

  • Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.

  • Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative. i would like to bookmark the page so i can come here again to read you. as you have done a wonderful job.

  • I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

  • I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

  • Gravatar Image.
    msvcr101 | 2020/10/20 20:13 | PERMALINK | EDIT/DEL | REPLY

    Hallo! Es tut mir leid, das ist nicht das Thema! Ich mag Ihr Forum wirklich! Vielen Dank, ich bin mit dir)

  • I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

  • I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information.

  • Gravatar Image.
    Live Crypto Price | 2020/10/25 14:57 | PERMALINK | EDIT/DEL | REPLY

    I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

  • Gravatar Image.
    yomimoka | 2020/10/25 15:35 | PERMALINK | EDIT/DEL | REPLY

    Positive site. where did u come up with the information on this posting? I'm pleased I discovered it though. ill be checking back soon to find out what additional posts you include.

  • I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.<a href="https://royalwallet789.com/register.html">สล็อต ออนไลน์? </a>

  • Gravatar Image.
    personalized jewelry | 2020/11/17 19:12 | PERMALINK | EDIT/DEL | REPLY

    Im no expert. but I believe you just made an excellent point. You certainly fully understand what youre speaking about. and I can truly get behind that.

  • Gravatar Image.
    tripleaces | 2020/11/18 11:20 | PERMALINK | EDIT/DEL | REPLY

    It was wondering if I could use this write-up on my other website. I will link it back to your website though.Great Thanks.

  • Gravatar Image.
    winflix | 2020/11/18 11:53 | PERMALINK | EDIT/DEL | REPLY

    I'm glad I found this web site. I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know. im always look for people to check out my web site.

  • Gravatar Image.
    CPAP Cleaners | 2020/11/18 12:30 | PERMALINK | EDIT/DEL | REPLY

    Thanks for sharing the info. keep up the good work going.... I really enjoyed exploring your site. good resource...

  • Gravatar Image.
    finance | 2020/11/21 17:13 | PERMALINK | EDIT/DEL | REPLY

    i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future..

  • Gravatar Image.
    spy game | 2020/11/22 18:36 | PERMALINK | EDIT/DEL | REPLY

    Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information..

  • Gravatar Image.
    fitness tips | 2020/11/22 22:39 | PERMALINK | EDIT/DEL | REPLY

    You have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!!

  • Gravatar Image.
    domain for sale | 2020/11/22 22:47 | PERMALINK | EDIT/DEL | REPLY

    Good post but I was wondering if you could write a litter more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!

Name
Password
Homepage
Secret