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

  • SEARCH
  • Home
  • 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 9274629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:23:54+00:00 2026-06-18T16:23:54+00:00

In VC2012, I want to create a mutex in a constructor using a unique

  • 0

In VC2012, I want to create a mutex in a constructor using a unique pointer and a deleter, so that I don’t need to create a destructor just to call CloseHandle.

I would have thought that this would work:

struct foo
{
    std::unique_ptr<HANDLE, BOOL(*)(HANDLE)> m_mutex;
    foo() : m_mutex(CreateMutex(NULL, FALSE, NULL), CloseHandle) {}
}

but on compiling I get an error:

error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,int 
(__cdecl *const &)(HANDLE)) throw()' : cannot convert parameter 1 from 
'HANDLE' to 'void *'

When I modify the constructor thus:

foo() : m_mutex((void*)CreateMutex(NULL, FALSE, 
    (name + " buffer mutex").c_str()), CloseHandle) {}

I get the even more unusual:

error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,
int (__cdecl *const &)(HANDLE)) throw()' : cannot convert 
parameter 1 from 'void *' to 'void *'

I’m at a loss now. HANDLE is a typedef for void*: is there some conversion magic I need to know about?

  • 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-06-18T16:23:55+00:00Added an answer on June 18, 2026 at 4:23 pm

    Forget about the custom deleter for now. When you say std::unique_ptr<T>, the unique_ptr constructor expects to receive a T*, but CreateMutex returns a HANDLE, not a HANDLE *.

    There are 3 ways to fix this:

    std::unique_ptr<void, deleter> m_mutex;
    

    You’ll have to cast the return value of CreateMutex to a void *.

    Another way to do this is use std::remove_pointer to get to the HANDLE‘s underlying type.

    std::unique_ptr<std::remove_pointer<HANDLE>::type, deleter> m_mutex;
    

    Yet another way to do this is to exploit the fact that if the unique_ptr‘s deleter contains a nested type named pointer, then the unique_ptr will use that type for its managed object pointer instead of T*.

    struct mutex_deleter {
      void operator()( HANDLE h ) 
      {
        ::CloseHandle( h );
      }
      typedef HANDLE pointer;
    };
    std::unique_ptr<HANDLE, mutex_deleter> m_mutex;
    foo() : m_mutex(::CreateMutex(NULL, FALSE, NULL), mutex_deleter()) {}
    

    Now, if you want to pass a pointer to function type as the deleter, then when dealing with the Windows API you also need to pay attention to the calling convention when creating function pointers.

    So, a function pointer to CloseHandle must look like this

    BOOL(WINAPI *)(HANDLE)
    

    Combining all of it,

    std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                    BOOL(WINAPI *)(HANDLE)> m_mutex(::CreateMutex(NULL, FALSE, NULL),
                                                    &::CloseHandle);
    

    I find it easier to use a lambda instead

    std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                    void(*)( HANDLE )> m_mutex;
    foo() : m_mutex(::CreateMutex(NULL, FALSE, NULL), 
                    []( HANDLE h ) { ::CloseHandle( h ); }) {}
    

    Or as suggested by @hjmd in the comments, use decltype to deduce the type of the function pointer.

    std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                    decltype(&::CloseHandle)> m_mutex(::CreateMutex(NULL, FALSE, NULL),
                                                      &::CloseHandle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write an XNA game using .NET 4.5, so that I can
I just inherited a SharePoint project that I want to refactor and take all
I need to write a procedure in assembler, that calculates the value of a
I am using VS2012 and my project is 4.0, but I want to use
I have a vector of string. I want to be able to search that
I just want to post this here to make sure I'm not missing something
I'm new to web security so I don't want to implement my own. I
Say I have these 3 different functions that I may want to point to:
I am using VS2012 and I want to set thread priority from within a
So I've just picked up VS2012 and I want to start an ASP.NET MVC

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.