Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:07:03+00:00 2026-05-12T17:07:03+00:00

in my code I use HANDLE s from windows.h . They are used like

  • 0

in my code I use HANDLEs from windows.h. They are used like

HANDLE h;
if (!openHandleToSomething(arg1, arg2, &h)) {
    throw std::exception("openHandleToSomething error");
}
/* Use the handle in other functions which can throw as well */
if (!CloseHandle(h)) {
    throw std::exception("closeHandle error");
}

As you see, you have to insert this CloseHandle to every exception which can happen in the middle of acquiration and release. Therefore, it’s likely you forget one (or there is a fancy SEH exception which you didn’t know about) and voilà, you have your memory leak.

Recently, I’ve read about RAII which should remove the headaches from such cases and should call this CloseHandle automatically. I’ve also seen that there is something like std::auto_ptr<someType> in C++ which solves the problem for resources which were allocated with new.

However, since I don’t use new and since HANDLE is just typedefed to be a void *, I wonder how I should use the std::auto_ptr<someType>. Somehow, it should be possible to give it a custom deleter function (if (!CloseHandle(h)) { throw std::exception("closeHandle error"); }). Creating a class would be another method since the destructor gets called any time an instance of it gets out of scope. However, it’s just overkill to have a class for every simple thing.

How can I fix these accidential memory leaks?

Note that I would prefer solutions which are in pure C++ with no libraries and big dependencies except if they are really small and used in most of the environments anyways.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:07:04+00:00Added an answer on May 12, 2026 at 5:07 pm

    One idea that comes to mind is to use boost::shared_ptr with a custom deleter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to handle failed requests. - (void)requestFailed:(ASIHTTPRequest *)request { [self.alertView
When I use the Windows API call GetFileSizeEx() from my Delphi 6 app on
I am porting some code from windows to Linux (Ubuntu 9.10). I have a
I was studying the following piece of code from WatiN which handles launching and
I use the OpenOffice API from my Java Program to handle Documents for me.
I found code online that displays how to use threads from a tutorial by
Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
I have the following code: use Imager::Screenshot 'screenshot'; my $img = screenshot(hwnd => 'active',
The original Code: 'use strict'; function GitJs(config) { var defaults = { inheriting: false,
I know this code use to work, I must have accidentally changed something when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.