• 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
MarkS

Strange problem with delete with VS 2012.

13 posts in this topic

According to everything I have read, I should be able to call delete on a pointer more than once without a crash. I recently ran into an issue with Visual Studio where the destructor was called more than once. That was due to a problem in my code and was corrected, but oddly, this caused a crash. If I did this, there was no crash:

 

[code]

~destructor()

{
if(pointer != NULL)

{

delete pointer;

pointer = NULL;

}
}

[\code]

 

This isn't supposed to be necessary, correct? Is Visual Studio screwed up or is this supposed to happen?

Edited by MarkS
0

Share this post


Link to post
Share on other sites

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

0

Share this post


Link to post
Share on other sites

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

 

Pointers do not contain unsigned chars or image data. Pointers point to a location in memory. /pedantic

 

More importantly, what is going on in your program that your structure is encountering objects that may or may not be deleted?

0

Share this post


Link to post
Share on other sites

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

 

Pointers do not contain unsigned chars or image data. Pointers point to a location in memory. /pedantic

 

More importantly, what is going on in your program that your structure is encountering objects that may or may not be deleted?

Yeah, I mistyped. :o I was remembering the code (unsigned char *image_data) as I typed and typed that.

As to the question, I am going from memory and I don't remember exactly what was going on. I do know that the code was structured badly and I corrected it. My confusion was in not remembering that it is OK to delete a pointer set to NULL. I thought it was OK to delete a pointer more than once.

To be honest, I have always used raw pointers for image data. I really never gave smart pointers much thought. I'll experiment with them tonight.

0

Share this post


Link to post
Share on other sites

In case "pointer" is a member of the object, I would be extremely worried if the bug disappears, just because it's set to null after delete. In fact, I would strongly advice _against_ setting member pointers to null in your destructor, exactly because it can hide more severe bugs. Doing so should have absolutely zero effect on program execution, unless something else is wrong (like deleting the actual object twice). In that case, reliably crashing is the best that can happen, as it clearly shows that something is wrong (instead of causing weird and inexplicable behavior down the road).

2

Share this post


Link to post
Share on other sites

I use container classes, but smart pointers are a little overkill for this application.

 

No, they're really not. Seriously, whenever you find yourself writing a delete in a destructor, stop, and use a smart pointer instead.

 

The overhead is minimal and in some cases non-existent, the code is more intention revealing (the pointer type will tell you the ownership semantics), and you don't have to remember to delete them.

 

It's a win win situation.

 

Put it this way, if I asked you to hang up a picture, would you say "it's just one nail, a hammer is overkill here, I'll just hit it with my forehead"?

 

Whilst I agree that smart pointers clear up the semantics of the code, in real production code I have not really seen them in use so far sadly though.
 

Edited by NightCreature83
0

Share this post


Link to post
Share on other sites

That's often one of two problems. First, code bases have a tendency to be ancient. std::auto_ptr was often a heptagon in a world of mainly round and square holes. Template support was very shaky on early compilers (and still is on some platforms). Boost was not always an option.

 

Secondly, a sadly high number of 'C++ programmers' have decided to call themselves that after reaching the 'C with classes' state and never evolved from there.

Edited by BitMaster
2

Share this post


Link to post
Share on other sites

Secondly, a sadly high number of 'C++ programmers' have decided to call themselves that after reaching the 'C with classes' state and never evolved from there.

 

That, sadly, describes me. However, I am working to rectify that.

 

I implemented smart pointers in my code in just a few minutes and I have to say that I like them. Once I got past the weird initialization, compared to just calling new on a raw pointer, it actually makes sense and is much more convenient than raw pointers.

Edited by MarkS
0

Share this post


Link to post
Share on other sites

Secondly, a sadly high number of 'C++ programmers' have decided to call themselves that after reaching the 'C with classes' state and never evolved from there.

Sadly, there is now also a breed of programmer who have learned that "a raw pointer is a bug in progress", but only learned to use a single smart_ptr class.

 

It bugs me no end to see an externally-reference-counted smart pointer implementation being used to scope non-shared instance variables...

That is what I was originally thinking and one reason that I did not use smart pointers. The class and associated member pointer is not shared. However, I suppose this is the purpose of unique_ptr, which is what I used when I converted my code to use smart pointers.

 

Raw pointers are not evil, they just complicate memory management. In fact, I would say this push towards using smart pointers in exclusion to raw pointers is creating a generation of programmers that do not understand how to do proper memory management, i.e., the compiler does it for me, so why should I care how to do it?

Edited by MarkS
0

Share this post


Link to post
Share on other sites

Sadly, there is now also a breed of programmer who have learned that "a raw pointer is a bug in progress", but only learned to use a single smart_ptr class.
 
It bugs me no end to see an externally-reference-counted smart pointer implementation being used to scope non-shared instance variables...

Well, pre-C++11 there was not really a good way to deal with non-shared managed pointer. std::auto_ptr was not up to the job in most cases. boost::scoped_ptr was noncopyable (although swappable) which made quite a few potential uses annoying and/or awkward.

I'm a bit disappointed though that something like boost::intrusive_ptr didn't make it into C++11. Edited by BitMaster
0

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