• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Six222

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

    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
0

Share this post


Link to post
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.

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.

2

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
Share on other sites

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

0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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

or whatever
0

Share this post


Link to post
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.

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?

0

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0