Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalid data in objects created on stack C++
    primarykey
    data
    text
    <p>I couldn't really find any better title for this question. </p> <p>I have three classes: <code>CheckBox</code>, <code>Button</code> and <code>Background</code>.</p> <pre><code>class CheckBox : public Component { private: Button m_button; public: CheckBox(const Point &amp;pos, int width, int height, std::string text); CheckBox(); }; CheckBox::CheckBox(const Point &amp;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&amp; pos, Background* pBg, Background* pBgOnClick, int width, int height, int fontSize, std::string title); ~Button(); }; Button::Button(const Point&amp; 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&lt;unsigned char&gt; m_pImageData; unsigned int m_width; unsigned int m_height; GLuint m_texture; bool load(const std::string&amp; pFileName); public: Background(const std::string&amp; pFileName, bool bTiling); ~Background(); bool draw(const Point &amp;pos, unsigned int width, unsigned int height); bool draw(const Point &amp;pos); }; Background::Background(const string&amp; pFileName, bool bTiling) : m_bTiling(bTiling), m_pFileName(pFileName) { load(pFileName); } </code></pre> <p>So as you can see, <code>CheckBox</code> class includes <code>Button m_button</code> and <code>Button</code> class includes <code>Background* m_pBg</code>. In <code>Background</code> constructor I load image data and store it in <code>std::vector</code>, it doesn't really matter - I know it's working, because it was already checked. </p> <p>When I create <code>CheckBox</code> object, the data inside it's <code>m_button</code> 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 <code>"Error reading characters in string"</code>. 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.</p> <p>When I changed <code>m_button</code> field in <code>CheckBox</code> class to be created on a heap (pointer to <code>Button</code>, object created by using <code>new</code> operator) everything seems to be working just fine. Data is being loaded properly and remains like this.</p> <p>Could anyone please explain me what can be the reason of such problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload