Followers 0

# C++11 Warning C4150 and shared_ptr usage with Allegro

## 12 posts in this topic

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();

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
0

##### Share on other sites

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.

(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.

2

##### Share on other sites

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

0

##### Share on other sites

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

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.
1

##### Share on other sites

(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.
0

##### Share on other sites

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

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.

0

##### Share on other sites
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.
0

##### Share on other sites

(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).
1

##### Share on other sites

(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.
0

##### Share on other sites
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.

or whatever
0

##### Share on other sites

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.

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?

0

##### Share on other sites
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 [tt]new[/tt] 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.
1

## Create an account

Register a new account