Jump to content

  • Log In with Google      Sign In   
  • Create Account


Program doesnt work with whole program optimization


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
8 replies to this topic

#1 Waterlimon   Crossbones+   -  Reputation: 2456

Like
0Likes
Like

Posted 09 February 2013 - 12:04 PM

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.

Attached Thumbnails

  • Buggg.png

o3o


Sponsor:

#2 iMalc   Crossbones+   -  Reputation: 2299

Like
1Likes
Like

Posted 09 February 2013 - 01:00 PM

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, 09 February 2013 - 01:25 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#3 Waterlimon   Crossbones+   -  Reputation: 2456

Like
0Likes
Like

Posted 09 February 2013 - 01:19 PM

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

o3o


#4 MichaBen   Members   -  Reputation: 481

Like
1Likes
Like

Posted 09 February 2013 - 03:28 PM

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.



#5 iMalc   Crossbones+   -  Reputation: 2299

Like
0Likes
Like

Posted 09 February 2013 - 04:14 PM

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, 09 February 2013 - 04:16 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#6 Waterlimon   Crossbones+   -  Reputation: 2456

Like
0Likes
Like

Posted 09 February 2013 - 04:19 PM


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.

o3o


#7 iMalc   Crossbones+   -  Reputation: 2299

Like
1Likes
Like

Posted 09 February 2013 - 04:43 PM

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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#8 AllEightUp   Moderators   -  Reputation: 4180

Like
1Likes
Like

Posted 10 February 2013 - 01:45 AM

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



#9 Waterlimon   Crossbones+   -  Reputation: 2456

Like
1Likes
Like

Posted 10 February 2013 - 09:43 AM

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.


o3o





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