Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. std::shared_ptr causing C4150 warning even if custom deleter is used in Visual C++ 2012 [Solved]

std::shared_ptr causing C4150 warning even if custom deleter is used in Visual C++ 2012 [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonperformancehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elerian
    wrote on last edited by
    #1

    Hello everybody, I am playing with LibSDL and tried to use smart pointers for some of the objects created by the Library (i.E. SDL_CreateWindow returns a SDL_Window* which I would like to put into a std::shared_ptr). For the rest of the post I will simple refer to SDL_Window but the same applies to other SDL objects. The SDL_Window structure in the SDL header is a forward declaration for a simple structure without a destructor (plain old data) but there is a function called SDL_DestroyWindow which destroyes the SDL_Window and frees the allocated memory. I use the following code to declare and initialize my smart pointer: std::shared_ptr<SDL_Window> window( SDL_CreateWindow( "Title", 0, 0, 640, 480, SDL_WINDOW_SHOWN ), SDL_DestroyWindow ); Now if I assign a nullptr to the initialized shared_ptr I get warning C4150 for deleting a pointer to the undefined or incomplete type SDL_Window but the SDL_DestroyWindow function gets called. Am I doing something wrong or will I have to ignore this warning? Thank you for your help, Andreas

    _ E 2 Replies Last reply
    0
    • E Elerian

      Hello everybody, I am playing with LibSDL and tried to use smart pointers for some of the objects created by the Library (i.E. SDL_CreateWindow returns a SDL_Window* which I would like to put into a std::shared_ptr). For the rest of the post I will simple refer to SDL_Window but the same applies to other SDL objects. The SDL_Window structure in the SDL header is a forward declaration for a simple structure without a destructor (plain old data) but there is a function called SDL_DestroyWindow which destroyes the SDL_Window and frees the allocated memory. I use the following code to declare and initialize my smart pointer: std::shared_ptr<SDL_Window> window( SDL_CreateWindow( "Title", 0, 0, 640, 480, SDL_WINDOW_SHOWN ), SDL_DestroyWindow ); Now if I assign a nullptr to the initialized shared_ptr I get warning C4150 for deleting a pointer to the undefined or incomplete type SDL_Window but the SDL_DestroyWindow function gets called. Am I doing something wrong or will I have to ignore this warning? Thank you for your help, Andreas

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      The warning is probably because of the forward declaration. Look at the documentation - Compiler Warning (level 2) C4150[^]

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      E 1 Reply Last reply
      0
      • _ _Superman_

        The warning is probably because of the forward declaration. Look at the documentation - Compiler Warning (level 2) C4150[^]

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        E Offline
        E Offline
        Elerian
        wrote on last edited by
        #3

        Hi! I am sure the forward declaration is the problem because you cannot delete a forward declarated type. The compiler simply does not know if it has the call a destructor if it has not been declared. But what I do not understand is why I get the warning if I supply a custom deleter. In this case the default delete (and therefor class destructor) should not be used by the shared_ptr template. If the last shared_ptr runs out of context it simply calls the provided custom deleter (which should know the full definition for the class it is deleting) Thank you for your response, but it does not hit the mark. However, I have found the problem (and the resolution) which I will explain in my next post. Andreas

        1 Reply Last reply
        0
        • E Elerian

          Hello everybody, I am playing with LibSDL and tried to use smart pointers for some of the objects created by the Library (i.E. SDL_CreateWindow returns a SDL_Window* which I would like to put into a std::shared_ptr). For the rest of the post I will simple refer to SDL_Window but the same applies to other SDL objects. The SDL_Window structure in the SDL header is a forward declaration for a simple structure without a destructor (plain old data) but there is a function called SDL_DestroyWindow which destroyes the SDL_Window and frees the allocated memory. I use the following code to declare and initialize my smart pointer: std::shared_ptr<SDL_Window> window( SDL_CreateWindow( "Title", 0, 0, 640, 480, SDL_WINDOW_SHOWN ), SDL_DestroyWindow ); Now if I assign a nullptr to the initialized shared_ptr I get warning C4150 for deleting a pointer to the undefined or incomplete type SDL_Window but the SDL_DestroyWindow function gets called. Am I doing something wrong or will I have to ignore this warning? Thank you for your help, Andreas

          E Offline
          E Offline
          Elerian
          wrote on last edited by
          #4

          Hello again, I now know what is causing the "problem" and will explain it. Example code: class MyIncompleteType; // forward declaration std::shared_ptr<MyIncompleteType> y(myAllocate(), myFree); y.reset(); // this works without warning (and calls myFree) y = nullptr; // this gives me warning C4150 The second parameter to the shared_ptr constructor (myFree) is used to create a specialized a custom deleter template which calles myFree if the object pointed to by shared_ptr has to be deleted. y.reset(); invokes the custom delimiter and sets the shared_ptr to empty (which is y = nullptr; initialized the shared_ptr with a new pointer to an object uf MyIncompleteType which is actually nullptr, but a standard deleter is created which causes to C4150 warning because it would delete the object using 'delete' (which is not going to happen because the new pointer is nullptr). So if you have to reset a shared_ptr of an incomplete type you simply have to use the reset() method. Assigning a new value to the shared_ptr will also assign a new deleter and if you assign a nullptr it will be boxed and the shared_ptr created will use a default deleter which causes the warning C4150. Andreas

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups