VS2010 x64:64位平台VS2010中包含错误数据的对象数组

时间:2015-11-13 10:32:12

标签: c++ visual-studio-2010 visual-c++ 32bit-64bit porting

我在VS2010中将32位代码移植到64位代码。我的应用程序在尝试启动其中一个对话框时崩溃,但我发现它在32位配置中完美运行,但在64位中却不行。我发现有一个包含字符数组的对象数组,在64位平台中数据不正确。例如。假设有一个字符串列表:红色,黑色,......在32位中,列表正确显示但在64位第二对象的字符指针指向黑色k,即比实际数据提前4个字节。对象大小在32位平台中为144字节,但在64位中,每个对象为148字节。

BOOL SomeDlg::OnInitDialog( HWND hwnd, HWND hwndFocus, LPARAM lParam )
{
   MEMBERASSERT();
   CABCDialog::OnInitDialog( hwnd, hwndFocus, lParam );

   int nHeight = ListBox_GetItemHeight( GetDlgItem(hwnd, IDC_DUMMYLIST),    -1 );
   m_myLst.SetItemHeight(-1, nHeight);
   m_ClrLst.SetItemHeight(-1, nHeight);

   char smyItem[64];
   for (UINT i=0; i < 10; i++)
   {

      LoadString(g_hInstance, IDS_COLORNAMES+i, smyItem, ELEMENTS_IN(smyItem));
      m_ZAItems[i].SetColor(m_pSettings->m_rgbColors[i]);
      m_ZAItems[i].SetText(smyItem);

    //debug issue, check size of object
      int a = sizeof(m_ZAItems[i]);
      m_myLst.AddItem( &m_ZAItems[i] );
    }

//some other stuff
}

  class SomeDlg : public CABCDialog
   {
   private:
      MyDrwaLstBox m_myLst;
      My_ColorComboBox    m_ClrLst;
      My_ColorText  m_ZAItems[11];


   private:    
   SomeDlg();
   SomeDlg(const SomeDlg &);
   const SomeDlg &operator=(const SomeDlg &);


  public:   
     SomeDlg(HWND hwndZW, MCURRENT* pSettings , LPCSTR szName );
     virtual void Notification( ADM_Wnd *pWnd, UINT uID, int nNotificationCode, LPARAM lData );
    virtual BOOL OnInitDialog( HWND hwnd, HWND hwndFocus, LPARAM lParam );
  };

class MyDrwaLstBox : public MY_ListBox
{
protected:
   BOOL                          m_bpmn;
private:
                             MyDrwaLstBox( const  MyDrwaLstBox& );
   const MyDrwaLstBox &  operator=( const MyDrwaLstBox & );

public:
                             MyDrwaLstBox ();
   virtual void                  OnDrawItem( HWND hwnd,
                                         const DRAWITEMSTRUCT * lpDrawItem );
};


class My_ColorText : public My_Color
{
private:                                 
   enum { txtLen = 128 };    
   TCHAR m_szText[ txtLen ]; 

   My_ColorText ( const My_ColorText  & );
   const My_ColorText  &operator=( const My_ColorText  & );


public:
   My_ColorText ();
   My_ColorText ( COLORREF rgbColor, LPCTSTR pszText );

   void SetText( LPCTSTR pszText );
   void SetText( HINSTANCE hInstance, UINT uID );
   LPCTSTR GetText();

   virtual void Draw( HDC hDC, RECT rRect, BOOL bChecked, BOOL bDisabled,
                  BOOL bFocus, BOOL bGrayed, BOOL bSelected );
};

My_ColorText::My_ColorText()
{   
   m_szText[0] = '\0';
}

My_ColorText::My_ColorText( COLORREF rgbColor, LPCTSTR pszText )
 : My_Color( rgbColor )
{
   MDC_strncpyz( m_szText, pszText );
}

void My_ColorTex::SetText( LPCTSTR pszText )
{
  MDC_strncpyz( m_szText, pszText );
}

void My_ColorText::SetText( HINSTANCE hInstance, UINT uID )
{
  *m_szText = '\0';
  LoadString( hInstance, uID, m_szText, ELEMENTS_IN( m_szText ) );
}

LPCTSTR My_ColorText::GetText()
{
  return m_szText;
}

template <size_t size>
LPSTR MDC_strncpyz(CHAR (&dest)[size], LPCSTR srce) { return MDC_strncpyz(dest, srce, size); }


class My_Color : public My_DrawItem
{
private:
   COLORREF                   m_rgbColor;

private:
                          My_Color( const My_Color & );
  const My_Color &    operator=( const My_Color & );

public:
                          My_Color( void );
                          My_Color( COLORREF rgbColor );

  virtual void               Draw( HDC hDC, 
                                RECT rRect,
                                BOOL bChecked,
                                BOOL bDisabled,
                                BOOL bFocus,
                                BOOL bGrayed,
                                BOOL bSelected );
};

My_Color::My_Color void )
 : m_rgbColor( RGB_BLACK )
{
}

My_Color::My_Color( COLORREF rgbColor )
 : m_rgbColor( rgbColor )
{
}

 class My_DrawItem
{
protected:                             
   BOOL                       m_bInListBox;                              
   DWORD                      m_dwUser;
private:
                          My_DrawItem( const My_DrawItem & );
const My_DrawItem &  operator=( const My_DrawItem & );

public:
                          My_DrawItem();
virtual                    ~My_DrawItem();
virtual void               Draw( HDC hDC, 
                                RECT rRect,
                                BOOL bChecked,
                                BOOL bDisabled,
                                BOOL bFocus,
                                BOOL bGrayed,
                                BOOL bSelected );
};

My_DrawItem::My_DrawItem()
 : m_bInListBox( FALSE ),
m_dwUser( 0 )
{
}

&#39; SomeDlg&#39; class包含My_ColorComboBox类的对象,该类又包含类My_ColorText的对象数组。  当通过My_ColorComboBox类调用My_ColorText类的构造函数时,m_ColorItems [0]的m_szText和m_ColorItems [1]的m_szText的基址的差异等于152字节。意味着My_ColorText()构造函数为不同的152字节的m_szText变量分配基址。  但是当My_ColorText类的构造函数通过SomeDlg类调用时,m_ZAItems [0]的m_szText和m_ZAItems [1]的m_szText的基址的差异等于148字节。因此,字符串不会插入到152字节差异的适当位置。

当我检查sizeof(My_ColorText)时,在两种情况下它都给我152个字节。

1 个答案:

答案 0 :(得分:0)

我在堆上创建了对象,而不是在堆栈上创建。 下面的行已添加解决问题。

for (UINT i=0; i < 10; i++)
{
   LoadString(g_hInstance, IDS_COLORNAMES+i, smyItem, ELEMENTS_IN(smyItem));
   //m_ZAItems[i].SetColor(m_pSettings->m_rgbColors[i]);
   //m_ZAItems[i].SetText(smyItem);

   myLst.AddItem( new  My_ColorText( m_pSettings->m_rgbColors[i], smyItem ) );
}