I've got a piece of code that's causing strange problems when compiled in release (using Visual Studio 2010). Am I violating some rule in the C++ standard by doing this?

Object* obj = new Object( ++m_id, name );
m_hashmap->add( m_id, obj );
This code is being run inside a function in a factory object, and m_id is a signed int initialized to -1 in the factory's constructor.

When compiled with optimizations turned off (debug build), this code works fine (m_id is incremented to 0 before being passed to the constructor of Object, and then the object is added to the hash map with an id of 0).

When compiled with optimizations turned on (release build), I see very strange things: 0 is passed to the constructor of Object, which is correct. However, the add function is then called with -1 being passed in as the id, which results in an assert (and using the debugger is mostly worthless, the values of the ints being passed to the functions show up grayed out and as some large negative number).

The "fix" is simple - just increment m_id on the previous line and all works like it should (I'm guessing that declaring m_id as volatile would also work, but I've never actually had to use the volatile keyword before).

So my question is, am I actually violating the standard? Or is this an optimization bug? Or is something else going on?