Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++11 Warning C4150 and shared_ptr usage with Allegro


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Six222   Members   -  Reputation: 439

Like
0Likes
Like

Posted 28 February 2013 - 08:28 AM

I'm a little confused at the usage of a shared_ptr.. this code works but i'm not sure if a) it's safe or b) it's correct

Decleration:

 std::map<const std::string, std::shared_ptr<ALLEGRO_BITMAP>> BitmapList;
 

Code:

const std::shared_ptr<ALLEGRO_BITMAP> BitmapManager::AddBitmap(const std::string filePath)
{
    std::string fullPath = "Assets/Images/";
    fullPath += filePath.c_str();

    BitmapList[filePath] = std::shared_ptr<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()));

    if(BitmapList[filePath] != nullptr)
        return BitmapList[filePath];
    else
        return nullptr;
}
 

Usage:

std::shared_ptr<ALLEGRO_BITMAP> b = Bitmap->AddBitmap("test.bmp")
al_draw_bitmap(b.get(), 30, 30, 0)
 

and when compiling in MSVC++ 10 I get these build warnings:

 

1>c:\program files\microsoft visual studio 10.0\vc\include\memory(1664): warning C4150: deletion of pointer to incomplete type 'ALLEGRO_BITMAP'; no destructor called
1>          c:\allegro\include\allegro5\mouse.h(99) : see declaration of 'ALLEGRO_BITMAP'
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1448) : see reference to function template instantiation 'void std::tr1::shared_ptr<_Ty>::_Resetp<_Ty>(_Ux *)' being compiled
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP,
1>              _Ux=ALLEGRO_BITMAP
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1446) : while compiling class template member function 'std::tr1::shared_ptr<_Ty>::shared_ptr(std::nullptr_t)'
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP
1>          ]
1>          \\b-all-data03\data03\2\10544082\documents\visual studio 2010\projects\allegrotest\allegrotest\bitmapmanager.cpp(14) : see reference to class template instantiation 'std::tr1::shared_ptr<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP
1>          ]
1>c:\program files\microsoft visual studio 10.0\vc\include\memory(1107): warning C4150: deletion of pointer to incomplete type 'ALLEGRO_BITMAP'; no destructor called
1>          c:\allegro\include\allegro5\mouse.h(99) : see declaration of 'ALLEGRO_BITMAP'
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1106) : while compiling class template member function 'void std::tr1::_Ref_count<_Ty>::_Destroy(void)'
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1662) : see reference to class template instantiation 'std::tr1::_Ref_count<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\memory(1448) : see reference to function template instantiation 'void std::tr1::shared_ptr<_Ty>::_Resetp<_Ty>(_Ux *)' being compiled
1>          with
1>          [
1>              _Ty=ALLEGRO_BITMAP,
1>              _Ux=ALLEGRO_BITMAP
1>          ]

Edited by Six222, 28 February 2013 - 08:29 AM.


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3658

Like
3Likes
Like

Posted 28 February 2013 - 08:38 AM

I'm not familiar with Allegro but I highly doubt you are using it correctly. If a library allocates anything via a factory function (like al_load_bitmap), then it will generally require you to delete it via a library-dependent delete function (practically never the standard delete operator).
You need to specify the deleter to use, for example

BitmapList[filePath] = std::shared_ptr<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()), [] (ALLEGRO_BITMAP* bm) { al_delete_bitmap(bm); });
This code assumes al_delete_bitmap is indeed the Allegro counterpart to al_load_bitmap.

#3 Bregma   Crossbones+   -  Reputation: 4748

Like
2Likes
Like

Posted 28 February 2013 - 08:40 AM

Well, the code you've posted looks (mostly) correct, and should be safe, but it sounds like you don't have the destructor declaration for your ALLEGRO_BITMAP type in scope where you need it.

 

By (mostly), there are two things.

 

(1) Always use std::make_shared where you can, instead of the new operator.

BitmapList[filePath] = std::make_shared<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()));

 

(2) Do not return nullptr when you mean to return std::shared_ptr<ALLEGRO_BITMAP>() -- they're not al all the same thing.  That's probably the source of your trouble.


Stephen M. Webb
Professional Free Software Developer

#4 Six222   Members   -  Reputation: 439

Like
0Likes
Like

Posted 28 February 2013 - 08:46 AM

Ah that works, thanks BitMaster and thanks for the comments Bregma :)



#5 BitMaster   Crossbones+   -  Reputation: 3658

Like
1Likes
Like

Posted 28 February 2013 - 08:56 AM

(1) Always use std::make_shared where you can, instead of the new operator.
BitmapList[filePath] = std::make_shared<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()));

I don't see the point of this. The idea behind std::make_shared is to avoid two allocations for new (one for the shared_ptr control structure, one for the actual object), but in the case of al_load_bitmap you are completely unable to combine allocations. Looking at the warnings the OP gets, the ALLEGRO_BITMAP type is completely opaque. Setting aside it needs to be allocated in a different runtime (whichever runtime the Allegro library uses), you don't know anything about it, not even the size.

#6 King Mir   Members   -  Reputation: 1892

Like
0Likes
Like

Posted 28 February 2013 - 09:28 AM

(2) Do not return nullptr when you mean to return std::shared_ptr() -- they're not al all the same thing. That's probably the source of your trouble.

I don't agree with this. The code does the same thing either way. The source of the trouble is that ALLEGRO_BITMAP is an incomplete type.

#7 patrrr   Members   -  Reputation: 937

Like
0Likes
Like

Posted 28 February 2013 - 09:28 AM

(1) Always use std::make_shared where you can, instead of the new operator.
BitmapList[filePath] = std::make_shared<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()));

I don't see the point of this. The idea behind std::make_shared is to avoid two allocations for new (one for the shared_ptr control structure, one for the actual object), but in the case of al_load_bitmap you are completely unable to combine allocations. Looking at the warnings the OP gets, the ALLEGRO_BITMAP type is completely opaque. Setting aside it needs to be allocated in a different runtime (whichever runtime the Allegro library uses), you don't know anything about it, not even the size.

std::make_shared is also for exception safety, though it's not needed here. But I'd say using make_* is a good habit to have.



#8 BitMaster   Crossbones+   -  Reputation: 3658

Like
0Likes
Like

Posted 28 February 2013 - 09:35 AM

I still fail to see how to use std::make_shared in this context. According to the Allegro documentation ALLEGRO_BITMAP is an incomplete type (which was already nearly certain considering the OPs warnings). An incomplete type does not have a constructor, neither does the compiler know its size.
Of course you should generally use std::make_shared, but it's as far as I can tell, it's impossible to use std::make_shared in this context.

#9 BitMaster   Crossbones+   -  Reputation: 3658

Like
1Likes
Like

Posted 28 February 2013 - 09:45 AM


(2) Do not return nullptr when you mean to return std::shared_ptr() -- they're not al all the same thing. That's probably the source of your trouble.

I don't agree with this. The code does the same thing either way. The source of the trouble is that ALLEGRO_BITMAP is an incomplete type.


Actually, while the incomplete type is indeed the root cause of the problem here (and also the reason std::make_shared is a no-go), it's often a bad idea to construct any shared_ptr using a null_ptr (or 0 for boost::shared_ptr in pre-C++11). When you construct a shared_ptr with a nullptr instead of default constructing it, the deleter will be called when the object is freed. That's not an issue with the default deleter because delete null_ptr is legal, other deleters might not have this property (something I learned the hard way with GDAL objects).

#10 King Mir   Members   -  Reputation: 1892

Like
0Likes
Like

Posted 28 February 2013 - 09:58 AM



(2) Do not return nullptr when you mean to return std::shared_ptr() -- they're not al all the same thing. That's probably the source of your trouble.

I don't agree with this. The code does the same thing either way. The source of the trouble is that ALLEGRO_BITMAP is an incomplete type.


Actually, while the incomplete type is indeed the root cause of the problem here (and also the reason std::make_shared is a no-go), it's often a bad idea to construct any shared_ptr using a null_ptr (or 0 for boost::shared_ptr in pre-C++11). When you construct a shared_ptr with a nullptr instead of default constructing it, the deleter will be called when the object is freed. That's not an issue with the default deleter because delete null_ptr is legal, other deleters might not have this property (something I learned the hard way with GDAL objects).


Interesting. Good to know.

#11 jwezorek   Crossbones+   -  Reputation: 1606

Like
0Likes
Like

Posted 28 February 2013 - 12:31 PM

Isn't the issue that OP needs to use a custom deleter to dispose of the allegro created pointer with the appropriate allegro function?

i.e.

BitmapList[filePath] = std::shared_ptr<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()), al_destroy_bitmap);

or whatever

#12 Six222   Members   -  Reputation: 439

Like
0Likes
Like

Posted 28 February 2013 - 01:51 PM

Isn't the issue that OP needs to use a custom deleter to dispose of the allegro created pointer with the appropriate allegro function?

i.e.

BitmapList[filePath] = std::shared_ptr<ALLEGRO_BITMAP>(al_load_bitmap(fullPath.c_str()), al_destroy_bitmap);

or whatever

Yeah that was the answer, but as everyone seems to be saying I'm curious as to why std::make_shared should be used?



#13 SiCrane   Moderators   -  Reputation: 9387

Like
1Likes
Like

Posted 28 February 2013 - 06:08 PM

make_shared allocates the object and the reference count together in a single allocation. This reduces book keeping overhead and makes things easier to reason about with regards to exception safety. For purists it also has the advantage of getting rid of the keyword new from the code. For objects whose pointers are frequently copied at the same time as the objects are accessed, it also provides favorable cache access patterns.

On the downside, the memory block for the object isn't freed until all weak pointers to the object are destroyed as well. For long lived objects where shared_ptrs aren't frequently copied or have weak_ptrs made, the adjacent reference count information can reduce locality of access. For boost::make_shared on non-C++ compilers you can also have argument forwarding overhead.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS