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

Program doesnt work with whole program optimization

8 posts in this topic

It works in debug.

 

It also works in release, EXCEPT when full program optimization is enabled.

 

 

It is supposed to render chunks of tiles (theres a grid of them. The grid can be resized, causing them to be reallocated and some of the old ones assigned to the corresponding new ones in the newly allocated grid)

 

Now, when the full program optimization is on, the program compiles and works, but some of the chunks have messed up texcoords / vertex positions (there usually seems to be a pattern. Eg. all the texture coords in a chunk are offset or each tile misses the other triangle of the quad. usually the whole chunk is missing, and if there is a pattern its usually not the whole chunk, part of it is missing.)

 

I suspect this might have something to do with my questionable way of implementing assignment/copy construction.

I get the following warnings (level 4):

warning C4239: nonstandard extension used : 'argument' : conversion from 'a::b::blah' to 'a::b::blah &'

warning C4512: 'a::b::bleh' : assignment operator could not be generated

 

Basically i have a bunch of classes that contain resources of some kind (handles to opengl stuff, indices to pools...)

 

And since i want to move these objects from the old grid to the resized new grid, i do something like A=B (or A=T() for other stuff)

The resources are destroyed in the destructor generally, so to prevent that i do:

 

Class(Class& other)
{
*this=other;
}

operator=(Class& other)
{
swap(m_that, other.m_that)
return *this;
}
 

 

Can this cause such behaviour (is it undefined behaviour to modify the argument to operator= or copy constructor?) when full program optimization is on (MSVC++ 2010, building 32 bit)?

 

Any other possible reasons for such behaviour i should check?

 

Is the way i move resources upon assignment from one object to another undefined behaviour and is there a more correct way to do this (I asked about this some time ago, i believe i could do this with move semantics somehow, but this seemed to work too so i didnt care)?

 

Things i tried:

-Throw some #pragma pack(1) around to make sure the arrays of structs i pass to opengl arent messed up (i did change the calculation of stride so it shouldnt break even if there was/is padding)

-Tried stepping through the highly optimized release build. My window object was ok before passing a reference to a function and after it was full of garbage. Not sure if it was actually full of garbage or if i shouldnt step through optimized code xP

-Tried googling

-Tried getting rid of the warnings about not being able to generate assignment operators (i had a reference member in a bunch of classes, changed to pointers)

-Some other i cannot remember probably

 

thanks

 

edit: attached png of what it looks like (screen should be full of tiles)

 

edit: solved, adding move ctor and assignment operators and doing all moving of resources with std::move fixed it.

0

Share this post


Link to post
Share on other sites
Do not ever implement copy construction in terms of assignment. It must be the other way around.
The reason is that the copy-construction only has 1 job to do which is to copy the object, but asignment has two jobs to do - copy the object and free the current state. When using the assignment operator inside the copy-constructor, it will try and free something, but what is it freeing since the object hasn't been initialisaed yet?! That can cause problems.

Also, your assignment operators should take their argument by const reference. You should not be trying to modify the item you are copying from. That will cause problems.
Use the copy and swap idiom whenever you can, but use it correctly!

Those warnings are definitely things to sort out. Edited by iMalc
1

Share this post


Link to post
Share on other sites
But if i take it by const reference, how do i prevent the original object from deleting the resource upon destruction?
0

Share this post


Link to post
Share on other sites

Seems like you are trying to make your copy constructor into a move constructor. Although this will probably work (as far as I know, the compiler does recognize this as the copy constructor, and replaces the default copy constructor with const ref with your non-const ref one, trying to use it with a const object results in a compiler error), it's probably best to use a real move constructor this:

Class( const Class &other ); // Copy constructor, makes copies of the memory in a newly allocated location, leaves other intact.
Class( Class &&other ); // Move constructor: take the memory from other to this, and leave other in a state where the destructor won't free the memory you took anymore.

 

But I expect that indeed the problem lies in the copy constructor that calls assignment operator. Your assignment operator calls swap(m_that, other.m_that), but at that point m_that is uninitialized. I don't know what type it is, but if it's a pointer, you are swapping an uninitialized pointer with other, which will very likely result in undefined behavior, and undefined behavior is the biggest cause of why something crashes with certain compiler options and not with other options.

1

Share this post


Link to post
Share on other sites

But if i take it by const reference, how do i prevent the original object from deleting the resource upon destruction?

By using a shared_ptr to that resource.

It might be good if you would show some more of your code for some more specific help. Also make sure you know about, or learn, the rule of three. And for bonus points, this site has an excellent selection of articles about the nuances of C++: http://www.gotw.ca/gotw/ Edited by iMalc
0

Share this post


Link to post
Share on other sites


But if i take it by const reference, how do i prevent the original object from deleting the resource upon destruction?

By using a shared_ptr to that resource.

It might be good if you would show some more of your code for some more specific help. Also make sure you know about, or learn, the rule of three. And for bonus points, this site has an excellent selection of articles about the nuances of C++: http://www.gotw.ca/gotw/
I dont want to share the resource so shared pointer would be kind of a hack. And its an index to a pool of objects and not a raw pointer so i dont know how that would work out...

Ill make my code use move semantics instead and see if it helps.
0

Share this post


Link to post
Share on other sites
Then if the object being copied from or to is just a temporary, what you generally want it a unique_ptr instead.

However you have now stated that your resource is some kind of "handle" rather than an actual pointer. A move constructor and move assignment operator may do what you need, assuming you are using a compiler that supports them. However in that case you probably want to disable copy-construction and regular assignment because they cannot do what you need and if they were to be inadvertantly used then they would cause problems.
1

Share this post


Link to post
Share on other sites

'argument' : conversion from 'a::b::blah' to 'a::b::blah &'

 

This is almost guaranteed to be your problem.  As well as the whole program optimization system tends to work it unfortunately over-optimizes in a couple cases and usually they all revolve around proper construction order and temporaries.  Using something like "DoSomething( BlargoType( blah ) )" is actually a real serious pain in the ass for the compiler to deal with.  What it "should" mean to the compiler is:

 

BlargoType ___temporary( blah)

DoSomething( ___temporary );

 

Unfortunately when you start optimizing things over multiple functions, that syntactic sugar which VC allows and other compilers don't, comes up and bites ya in the ass.  It is illegal in other compilers for a reason, "scope" becomes questionable.  To put it simply, you see that warning, fix it.

1

Share this post


Link to post
Share on other sites

Moved all of my resource owning classes to use move semantics, prevented use of copy ctor and assignment operators (should probably implement those so they actually copy the resource for the few classes where it makes sense...) and used a lot of std::move, and it finally works properly with whole program optimization too.

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