locked
Custom Control Access Violation

    Question

  • I have a custom control that inherits from textbox in a separate class and file than my mainpage class. Inside my mainpage class I have a standard textblock that I'm using to output some variables. I'm trying to get a pointer to the mainpage class from my custom control so I can access the textblock and change its text. My mainpage class has the following function to get the textblock:

    Windows::UI::Xaml::Controls::TextBlock ^ GetTextBlock() { return OutputWindow; }

    Inside my custom control I have a function to retrieve the mainpage and textblock pointer which I call inside the mainpage constructor after InitializeComponent();.

    CustomControl:

    void CustomControl::SetMainPage(MainPage ^ mainPage)
    {
        m_mainPage = mainPage;
        m_outputWindow = mainPage->GetTextBlock();
    }

    MainPage:

    MainPage::MainPage()
    {
        InitializeComponent();
    
        m_customControl.SetMainPage(this);
    }

    The textblock pointer should be saved into the m_outputWindow pointer. But when I try to output a variable in the OnKeyDown event I get an access violation on m_outputWindow. Can someone show me the correct way to access the mainpage and textblock pointer so I can directly use the control from my custom control?
    Thursday, January 10, 2013 9:45 PM

Answers

  • The m_outputWindow member is going out of scope at the end of the CustomControl::SetMainPage function but the m_mainpage pointer is still valid. If I put the member declaration of m_outputWindow at the top of the source file everything works correctly, but I rather have the declarations inside the header, can anyone tell me what's going on?
    • Proposed as answer by Jesse Jiang Monday, January 14, 2013 6:16 AM
    • Marked as answer by Jesse Jiang Thursday, January 17, 2013 2:18 AM
    Friday, January 11, 2013 4:00 PM
  • Hi,

    We should get the m_mainpage in this way
     m_mainpage = MainPage::Current;

    And we should also check if the m_outputWindow is the member of your customer control class.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jesse Jiang Thursday, January 17, 2013 2:18 AM
    Monday, January 14, 2013 6:20 AM

All replies

  • The m_outputWindow member is going out of scope at the end of the CustomControl::SetMainPage function but the m_mainpage pointer is still valid. If I put the member declaration of m_outputWindow at the top of the source file everything works correctly, but I rather have the declarations inside the header, can anyone tell me what's going on?
    • Proposed as answer by Jesse Jiang Monday, January 14, 2013 6:16 AM
    • Marked as answer by Jesse Jiang Thursday, January 17, 2013 2:18 AM
    Friday, January 11, 2013 4:00 PM
  • Hi,

    We should get the m_mainpage in this way
     m_mainpage = MainPage::Current;

    And we should also check if the m_outputWindow is the member of your customer control class.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jesse Jiang Thursday, January 17, 2013 2:18 AM
    Monday, January 14, 2013 6:20 AM