C++ Boolean Variables Changing -
i have c++ class; class follows:
first, header:
class pagetableentry { public: pagetableentry(bool modified = true); virtual ~pagetableentry(); bool modified(); void setmodified(bool modified); private: pagetableentry(pagetableentry &existing); pagetableentry &operator=(pagetableentry &rhs); bool _modified; }; and .cpp file
#include "pagetableentry.h" pagetableentry::pagetableentry(bool modified) { _modified = modified; } pagetableentry::~pagetableentry() {} bool pagetableentry::modified() { return _modified; } void pagetableentry::setmodified(bool modified) { _modified = modified; } i set breakpoint on 3 lines in .cpp file involving _modified can see being set/changed/read. sequence goes follows:
- breakpoint in constructor triggered. _modified variable confirmed set true
- breakpoint in accessor triggered. _modified variable false!
this occurs every instance of pagetableentry. class not changing variable - else is. unfortunately, don't know what. class created dynamically using new , passed around (as pointers) various stl structures, including vector , map. mutator never called own code (i haven't gotten point yet) , stl structures shouldn't able to, , since breakpoint never called on mutator can assume aren't.
clearly there's "gotcha" private variables can, under circumstances, changed without going through class's mutator, triggered who-knows-what situation, can't imagine be. thoughts?
update: value of this @ each stage:
constructor 1: 0x100100210
constructor 2: 0x100100400
accessor 1: 0x1001003f0
accessor 2: 0x100100440
update2:
(code showing pagetableentry accessed)
// in constructor: _tableentries = std::map<unsigned int, pagetableentry *>(); // entry in table (body of testaddr() function, address unsigned int: std::map<unsigned int, pagetableentry *>::iterator it; = _tableentries.find(address); if (it == _tableentries.end()) { return null; } return (pagetableentry *)&(*it); // create new entry: pagetableentry *entry = testaddr(address); if (!entry) { entry = new pagetableentry(_currentprocessid, 0, true, kstoragetypedoesnotexist); _tableentries.insert(std::pair<unsigned int, pagetableentry *>(address, entry)); } those points @ pagetableentry objects stored , retrieved stl structures in order cause issue. other functions utilize testaddr() function retrieve entries.
unrelated: since c++ has 65663 questions, , far 164 have been asked today, means only today number of c++ tagged questions exceeded 16-bit unsigned integer. useful? no. interesting? yes. :)
either debugger isn't reporting value correctly (not unheard of, , expected in optimized builds) or have memory corruption elsewhere in program. code you've shown more-or-less fine , should behave expect.
edit corresponding 'update2':
problem in line:
return (pagetableentry *)&(*it); the type of *it std::pair<unsigned const, pagetableentry*>&, you're reinterpret-casting std::pair<unsigned const, pagetableentry*>* pagetableentry*. change line to:
return it->second; keep eye out other casts in codebase. needing cast in first place code smell, , result of doing cast incorrectly can undefined behavior, including manifesting memory corruption you're seeing here. using c++-style casts instead of c-style casts makes trivial find casts occur in codebase can reviewed (hint, hint).
Comments
Post a Comment