I have the following code, which loops through an array of menuoptions
and on each iteration, creates a ScaledRect
object and pushes it to a vector. This vector is a member of a struct.
I have verified that the ScaledRect is created with the correct values, yet when I print back the contents of the regions
vector ( in the second loop ), the loop never terminates and the values are garbage.
class ScaledRect : public Rect
{
public:
ScaledRect(int x1, int y1, int x2, int y2);
};
ScaledRect::ScaledRect(int x1, int y1, int x2, int y2):
_x1(x1), _y1(y1), _x2(x2), _y2(y2){}
// ScaledRect doesn't have copy constructor, but Rect does
Rect::Rect( const Rect &rect)
{
x1=rect.x1; y1=rect.y1; x2=rect.x2; y2=rect.y2; bClean=KD_FALSE;
}
typedef struct
{
std::vector<ScaledRect> regions;
}interface;
void PushRegions( interface * myself )
{
int i = 0;
while(menuoptions[i].callback != -1 )
{
ScaledRect s =
ScaledRect(menuoptions[i].x1,
menuoptions[i].y1,
menuoptions[i].x2,
menuoptions[i].y2);
myself->regions.push_back( s );
i++;
}
std::vector<ScaledRect>::iterator iter = myself->regions.begin();
std::vector<ScaledRect>::iterator done = myself->regions.end();
while(iter != done)
{
iter->Dump();
iter++;
}
}
EDIT
Please note - I've just edited - the memory for theinterface
is created and I do actually pass in the address of theinterface
to this function. (However, I have simplified those two lines here - what actually happens is that PushRegions
gets called via a ptr to a function, on a piece of newly allocated memory the size of an interface
).
I can't post all of the code here - but minimally its:
Func pfunc = GetPFuncForInterfaceObj();
size_t numbytes = GetSizeForInterfaceObj();
char memory = new char[numbytes];
pfunc(memory);
pfunc
ends up being PushRegions
and memory
ends up being passed as an interface
.
When I push the ScaledRect
object to a vector
declared at the top of PushRegions()
it works. Has anyone got any ideas why?