I couldn't really find any better title for this question.
I have three classes: CheckBox
, Button
and Background
.
class CheckBox : public Component
{
private:
Button m_button;
public:
CheckBox(const Point &pos, int width, int height, std::string text);
CheckBox();
};
CheckBox::CheckBox(const Point &pos, int width, int height, string text) :
Component(pos, width, height),
m_button(Button(Point(width-height,0), new Background("button_bg_sample.png", true), new Background("button_bg_onclick_sample.png", true), height, height, 10, "")),
{
}
class Button : public Component
{
private:
std::string m_text;
Background* m_pBackground;
Background* m_pBackgroundOnClick;
int m_fontSize;
public:
Button(const Point& pos, Background* pBg, Background* pBgOnClick, int width, int height, int fontSize, std::string title);
~Button();
};
Button::Button(const Point& pos, Background* pBg, Background* pBgOnClick, int width, int height, int fontSize, string title) :
Component(pos, width, height),
m_pBackground(pBg),
m_pBackgroundOnClick(pBgOnClick),
m_fontSize(fontSize),
m_text(title)
{
}
class Background
{
private:
std::string m_pFileName;
bool m_bTiling;
std::vector<unsigned char> m_pImageData;
unsigned int m_width;
unsigned int m_height;
GLuint m_texture;
bool load(const std::string& pFileName);
public:
Background(const std::string& pFileName, bool bTiling);
~Background();
bool draw(const Point &pos, unsigned int width, unsigned int height);
bool draw(const Point &pos);
};
Background::Background(const string& pFileName, bool bTiling) :
m_bTiling(bTiling),
m_pFileName(pFileName)
{
load(pFileName);
}
So as you can see, CheckBox
class includes Button m_button
and Button
class includes Background* m_pBg
. In Background
constructor I load image data and store it in std::vector
, it doesn't really matter - I know it's working, because it was already checked.
When I create CheckBox
object, the data inside it's m_button
is corrupted. When I try to check what's inside image data in debug mode, I get information that it's empty and the file name of that background is "Error reading characters in string"
. Though when I saw following step by step the code in debug mode, I've seen that the data was properly loaded inside constructor, but somehow when the object was created, data was already corrupted.
When I changed m_button
field in CheckBox
class to be created on a heap (pointer to Button
, object created by using new
operator) everything seems to be working just fine. Data is being loaded properly and remains like this.
Could anyone please explain me what can be the reason of such problem?