#### Archived

This topic is now archived and is closed to further replies.

# Optimization Flag causing problems...

This topic is 5061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The code below contains some classes (functor stuff, then two object classes just being used as examples). Below the classes, the code creates two functors using slightly different methods, both of which *should* work. Without the /O2 flag set, the code runs fine... it displays two message boxes, one saying HI!, then the next saying Hello. as it should. However, if the flag is set, both boxes display HI!. Also, if the first functor isn't created, the second one throws an exception - read the comments:
//functor stuff from Enginuity

class BaseFunctor
{
public:
virtual void operator()(void)=0;

};

template<class T>
class Functor : public BaseFunctor
{
protected:
T *obj;
typedef void (T::*fnType)(void);
fnType func;
public:
Functor(){}
Functor(T *o, fnType function)
{
func=function;
obj=o;
}

void operator ()(void)
{
(obj->*func)();
}

};

class SomeObject
{
public:
SomeObject(){}
~SomeObject(){}
void TheFunction()
{
MessageBox(0,"HI!","HI!",MB_OK);
}
};

class Object2
{
public:
Object2(){}
~Object2(){}
void TheFunction()
{
MessageBox(0,"Hello.","Hello.",MB_OK);
}
};

//... within the WinMain function

SomeObject myObj;
Object2 myObj2;

//If the next 4 lines are removed, the (*pBlah)() throws an Access violation exception

Functor<SomeObject> Bleh,*pBleh;
Bleh=Functor<SomeObject>(&myObj,&SomeObject::TheFunction);
pBleh = &Bleh;
(*pBleh)();
//

Functor<Object2> *pBlah = &Functor<Object2>(&myObj2,&Object2::TheFunction);
(*pBlah)();

//...


Anyone know what's going on? Tracking the problem down to this took me forever, and now that I've found it, I have no idea how to fix it . [EDIT] Added a little bit of description to the introduction to make it easier to understand what's going on... And here are a few more things I've figured out while playing with the code: My first thought had been that no memory was being allocated for the second functor, and it somehow ended up pointing at the first functor for some reason... but I tried adding some data to both classes, and when accessing that data through the functors (I made the obj members public to do this), it seems fine, so that's not it... [edited by - HappyDude on April 10, 2004 11:38:09 PM]

##### Share on other sites
Upon further messing around with the code, I've discovered that the functor pointers are actually pointing to exactly the same memory location (but only with /O2 set)... however, for reasons unknown, the data members within the object that the functors are pointing to are distinct (and are the way they should be). This makes no sense to me, because since both pointers are pointing to the same memory location, all of the data members accessed through the pointers should be exactly the same...

[EDIT]
To be more clear...
With /O2 set:
pBleh: (0x12fcb0) pBlah: (0x12fcb0)
pBleh->obj: (0x12fcb4) pBlah->obj: (0x12fcb4)
&pBleh->obj->theStr: (0x12fcf8) &pBlah->obj->theStr: (0x12fcdc)

Without it set:
pBleh: (0x12fe00) pBlah: (0x12fc7c)
pBleh->obj: (0x12fe04) pBlah->obj: (0x12fc80)
&pBleh->obj->theStr: (0x12fe48) &pBlah->obj->theStr: (0x12fe28)

[edited by - HappyDude on April 10, 2004 11:58:18 PM]