CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    50

    contructor not allocating array properly?

    This is a weird one... not sure why this is happening.

    In my .h, in my class, in the public section, I have the following:

    Code:
      CButton* useme;
    in my class constructor, I have:
    Code:
      useme = new CButton[MAX_WARP_TYPES];
    later in my program, I have:
    Code:
    void myClass::createScreenConfig(CString path,int num)
    {
       // ....
    
      useme[0].Create(_T(""),WS_CHILD|WS_VISIBLE|BS_CHECKBOX,
    		  CRect(rect.left+leftSpace, rect.top+topAdjust+fieldHeight*0,rect.right-3,rect.top+30),
    	          this,1500);
    
    }
    but this causes the following assert from wincore.cpp to fail:
    ASSERT(pWnd->m_hWnd == NULL); // only do once

    However, if I put the "useme = new CButton[MAX_WARP_TYPES];"

    right in the function, it works correctly. What's going on?

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote Originally Posted by ecspansion
    This is a weird one... not sure why this is happening.
    However, if I put the "useme = new CButton[MAX_WARP_TYPES];" right in the function, it works correctly. What's going on?
    It looks like createScreenConfig is called more than once - the assertion fails if you call Create() for a button that has already been created. If you put the new inside your function, you will just hide the bug, since new memory will be allocated before calling Create() every time the function is called.

  3. #3
    Join Date
    Apr 2004
    Posts
    76
    Hi ecspansion,

    I did not try to run the code, but I would probably approach it as follows:

    CButton** ppButton;
    *pButton = new CButton[ size ];

    then,

    pButton[0]->Create( ... )

    Jeff

  4. #4
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    50
    Quote Originally Posted by gstercken
    It looks like createScreenConfig is called more than once - the assertion fails if you call Create() for a button that has already been created. If you put the new inside your function, you will just hide the bug, since new memory will be allocated before calling Create() every time the function is called.
    DOH! There needs to be a ::smacks own head:: icon.

    Of course this is what's causing it - I don't know how I didn't see that before.

    Thanks!

    ... now where's that three hours just lost trying to find this bug?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured