Thread: access violation

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    access violation

    I am using a modeless dialog box to gather data and insert it into a CListCtrl and a CList.

    I Call the modeless dialog and pass "this" as a parameter.

    Inside my dialog constructer I save the the CWnd *pParent as CWnd *m_pParent.

    When the user presses "OK" I call the function in my original dialog using the folowing method.

    ((CMainDlg *)m_pParent)->SaveData(data,data,data);


    This all works fine. The problem is in my SaveData function. When it tries to access my CList or my CListCtrl I get an access violation.

    I have done this same operation before in another app and it worked fine. Anyone see a problem with what I am doing here?

    --Bonkey
    Best Regards,

    Bonkey

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463

    Re: access violation

    Originally posted by bonkey
    When the user presses "OK" I call the function in my original dialog using the folowing method.

    ((CMainDlg *)m_pParent)->SaveData(data,data,data);


    This all works fine. The problem is in my SaveData function. When it tries to access my CList or my CListCtrl I get an access violation.
    --Bonkey
    Hello,

    the description is vague for me, however, are you sure that the exception is thrown from SaveData?
    Is the explicit cast (CMainDlg *)m_pParent ok?
    The access violation shows that you are probably accessing an invalid pointer.

    Originally posted by bonkey
    I Call the modeless dialog and pass "this" as a parameter.
    Who is the caller? Which objects pointer are you passing to the function?

    Is there any error handling, exception handling in your code?

    Step through your code, check your pointers for validity; debugging might be the quickest way to find out what's wrong there.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    This isn't my exact code (i'm at work) but this should explain what I am doing.


    Code:
    CMainDlg::OpenChildWindow()
    {
      CChildDlg *dlg=new CChildDlg(this);
      dlg->Create();  //I don't remeber the options of the top of my head.
      dlg->ShowWindow(SW_SHOW);
    }
    
    CChildDlg::OnOk()
    {
      ((CMainDlg *)m_pParent)->SaveData(data);
    }
    
    CMainDlg::SaveData(CString data)
    {
       DataListCtrl.InsertItem(0,data);
    }
    The m_pParent is a pointer to CMainDlg. This is copied from the CChildDlg constructor. The constructor accepts the CWnd if it is passed.


    Please let me know if I am not giving you enough information. I can post my actual code in a few hours when I get home.

    BTW: This is MSVC6.

    Edit:

    I have stepped through the code. I get into SaveData fine. When I attempt to access the CListCtrl It complains that is does not have a valid m_hWnd.
    Last edited by bonkey; 11-19-2003 at 11:52 AM.
    Best Regards,

    Bonkey

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I think I may have figred it out.

    Code:
    OnOk()
    {
      UpdateData(true); // <------- Get the data from my edit.
      ((CMainDlg *)m_pParent->SaveData(data);
    }
    I just ran a test on my office computer with code to JUST do this and realized I may have left out that step. OOPS
    Best Regards,

    Bonkey

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Nope

    I'm home now and I had the UpdateData line.

    So, when I tried it at work it worked fine, at home No Go!

    I'm still stumped.
    Best Regards,

    Bonkey

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    More info:

    I added an UpdateData(true); at the beginning of SaveData()
    and I get this error:

    ASSERT(::IsWindow(m_hWnd)); // calling UpdateData before DoModal?

    I know the window is open as A) I can see it. B) It is the parent of the child window.

    I'm pulling my hair out.
    Best Regards,

    Bonkey

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    It MUST be something with the way I'm calling it.

    If I call SaveData() from the constructor of CMainDlg then it works fine. If I call it using the m_pParent pointer then I get the m_hWnd errors.

    I don't get it. Why would it work in one program and not the next?!?
    Best Regards,

    Bonkey

  8. #8
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Hi,


    The m_pParent is a pointer to CMainDlg. This is copied from the CChildDlg constructor. The constructor accepts the CWnd if it is passed.

    CChildDlg is public derived from CMainDlg, right?
    Code:
    Class CChildDlg : public CMainDlg
    Is DataListCtrl a public member of CMainDlg?


    If I get it correctly, m_pParent is a member of CChildDlg, initialized in CChildDlg's constructor with the CMainDlg this pointer, isn't it?

    If CChildDlg is derived from CMainDlg, why aren't you using a member of CMainDlg instead of initializing your own pointer within the constructor of CChildDlg? This way you avoid this casting, which can be risky (I haven't seen your class hierarchy, but explicit casting is never recommended):
    Code:
    ((CMainDlg *)m_pParent)->SaveData(data);


    P.s.
    this is rather a Windows issue, you should have post it there.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    no. It is derived from CDialog.

    That is a good idea though, may make my life a lot simpler.

    I did post this under windows, but posted here when I thought it was just a pointer problem. I am trying my best to post in the correct forum.
    Best Regards,

    Bonkey

  10. #10
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by bonkey
    no. It is derived from CDialog.
    Sorry, how is that?
    CChildDlg is derived from CDialog, and not CMainDlg??

    How do you cast then a CChildDlg* to CMainDlg*, when the two classes are not related at all? A dynamic_cast would throw an exception.
    Code:
    ((CMainDlg *)m_pParent)->SaveData(data);
    Is SaveData really executed - did you step in the function while debugging?

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Yes. It really is that way. Yes I have stepped through it.

    A rule of C++ says that if 2 classes are derived from the same class (both CMailnDlg and CChildDlg are derived from CDialog) then you can use a cast to access the sister class.

    At least the c++ class I took stated that. I can post a small working sample program. I wrote it just to prove that this works.


    --------------

    I tried deriving from CMainDlg but then MFC complained that CChildDlg was not derived from CDialog. Grr MFC can get in your way sometimes.
    Best Regards,

    Bonkey

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Here is a working sample.
    Best Regards,

    Bonkey

  13. #13
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by bonkey
    (both CMailnDlg and CChildDlg are derived from CDialog) then you can use a cast to access the sister class.
    Of course, I just missed the info that CMailnDlg and CChildDlg are derived from the same class (CDialog).

    I've got the code. But is not exactly our case, as only the base and one derived class is present.
    In our situation there are two derived classes.

    THe next question is how are these initialized, so if you'd provide the "wrong" code it would be useful.

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    The bad code is posted under the windows forum.

    Heres my code -- please help.
    Best Regards,

    Bonkey

  15. #15
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    I found the problem. I posted the solution in Windows forum, too.
    Irepeat myself, but you needed quick help

    In CLevelDlg::OnOK()
    m_pParent (CPayoutDlg) was used instead of m_pParentWnd. Just replace and everything is OK!

    Besides m_pParent was also uninitialized at this point (the value 0xcdcdcdcd means always "Red alert!" )
    Last edited by Carlos; 11-20-2003 at 09:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM