0

什么不对这个代码:E_OUTOFMEMORY,堆损坏

HRESULT AllocateAndFillOutParameters(DWORD * a_Attrib_Count, BSTR ** a_AttribName, DWORD** a_AttribId,BSTR** a_AttribDescription, BSTR** a_AttribDataType, 
    BSTR** a_AttribAdvanceDataType,DWORD** a_attribfieldlength, VECTOROFATTRIBUTESSTRUCT a_vectAttributes) 
{ 
HRESULT hr = S_OK; 
VECTOROFATTRIBUTESSTRUCT::iterator itr_vectCategory = a_vectAttributes.begin(); 

    // Memory Allocation 
    *a_Attrib_Count = a_vectAttributes.size(); 
    *a_AttribName = (BSTR*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(BSTR)); 
    *a_AttribDescription = (BSTR*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(BSTR)); 
    *a_AttribId = (DWORD*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(DWORD)); 
    *a_AttribDataType = (BSTR*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(BSTR)); 
    *a_AttribAdvanceDataType = (BSTR*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(BSTR)); 
    *a_attribfieldlength = (DWORD*)CoTaskMemAlloc(*a_Attrib_Count * sizeof(DWORD)); 
    // 

    for(int i=0 ;itr_vectCategory != a_vectAttributes.end(); itr_vectCategory++, i++) 
    { 
     *((*a_AttribName)+i) = SysAllocString(itr_vectCategory->m_strAttributeName.c_str()); 
     *((*a_AttribId)+i) = itr_vectCategory->m_dwAttributeId; 
     *((*a_AttribDescription)+i) = SysAllocString(itr_vectCategory->m_strAttributeDescription.c_str()); 
     *((*a_AttribDataType)+i) = SysAllocString(itr_vectCategory->m_strAttributeDataType.c_str()); 
     *((*a_AttribAdvanceDataType)+i) = SysAllocString(itr_vectCategory->m_strAttributeAdvanceDataType.c_str()); 
     *((*a_attribfieldlength)+i) = (itr_vectCategory->m_dwAttributeFieldLength); 

    } 

return hr; 
} 

它给堆损坏的运行时异常,我是在分配错误的方式? 感谢您的帮助。

+0

这是生产守则吗? – 2012-01-11 13:57:01

+1

在循环中,它应该真的是'(* a_AttribName)+ 1'吗?你不应该加'i'而不是'1'吗? – 2012-01-11 14:01:09

+0

是的,这是我在这里输入错误。 – Ehsank 2012-01-11 14:02:48

回答

0

我不能告诉你在哪里你的错误,但我会建议下载Debugging Tools for Windows然后reading the docs上启用使用gflags程序,它可以让你在那里堆损坏实际发生捕获错误页堆调试。