locked
Heap Corruption when memcpy on vertex buffer for Windows Phone 8 DirectX app

    Question

  • Hi,

    I have a Windows Phone 8 application running C#/XAML and DirectX.

    I created a vertex buffer where I want to update the data, but after I call a Map on the resource, and do a memcpy, the application breaks out on an error.

    D3D11 WARNING: ID3D11Texture2D::SetPrivateData: Existing private data of same name with different size found! [ STATE_SETTING WARNING #55: SETPRIVATEDATA_CHANGINGPARAMS] Critical error detected c0000374 TaskHost.exe has triggered a breakpoint. First-chance exception at 0x776EE881 (NTDLL.DLL) in TaskHost.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7771E130).

    In the code that calls a Map/Unmap, if I comment out this line, then the application continues running, no error.

     memcpy(verticesPtr, (void*)m_vertices, sizeOfVertices);

    // Initialize vertex array to zeros at first.
    	memset(m_vertices, 0, (sizeof(VertexType) * m_vertexCount));
    
    	// Now build the vertex array from the particle list array.  Each particle is a quad made out of two triangles.
    	int index = 0;
    
    	for(int i=0; i<m_currentParticleCount; i++)
    	{
    		// Bottom left.
    		m_vertices[index].position = XMFLOAT3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ);
    		m_vertices[index].texture =  XMFLOAT2(0.0f, 1.0f);
    		m_vertices[index].color =  XMFLOAT4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f);
    		index++;
    
    		// Top left.
    		// Bottom right.
    		// Bottom right.
    		// Top left.
    		// Top right.
    // JUST deleted some code here as it's the same as Bottom Left
    	}
    	
    	D3D11_MAPPED_SUBRESOURCE mappedResource;
    	
    	// Lock the vertex buffer.
    	DX::ThrowIfFailed(m_d3dContext->Map(m_vertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
    
    	// Get a pointer to the data in the vertex buffer.
    	VertexType * verticesPtr = (VertexType*)mappedResource.pData;
    
    	//// Copy the data into the vertex buffer.
    	int sizeOfVertices = sizeof(VertexType) * m_vertexCount;
    	memcpy(verticesPtr, (void*)m_vertices, sizeOfVertices);
    
    	//// Unlock the vertex buffer.
    	m_d3dContext->Unmap(m_vertexBuffer.Get(), 0);
    
    	return true;

    Member variables declarations

    Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer; struct VertexType { DirectX::XMFLOAT3 position; DirectX::XMFLOAT2 texture; DirectX::XMFLOAT4 color; }; VertexType* m_vertices;

    Wednesday, November 28, 2012 10:09 AM

Answers

  • Is the size of m_vertexBuffer at least sizeOfVertices? For debugging, do m_vertexBuffer->GetDesc( &desc ) and compare the returned size (desc.ByteWidth) to "sizeOfVertices".

    • Marked as answer by Elissa Tong Wednesday, November 28, 2012 3:43 PM
    Wednesday, November 28, 2012 2:53 PM

All replies

  • Is the size of m_vertexBuffer at least sizeOfVertices? For debugging, do m_vertexBuffer->GetDesc( &desc ) and compare the returned size (desc.ByteWidth) to "sizeOfVertices".

    • Marked as answer by Elissa Tong Wednesday, November 28, 2012 3:43 PM
    Wednesday, November 28, 2012 2:53 PM
  • You're right! Thanks! I created the buffer passing in sizeof(m_vertexBuffer) which is a pointer, 4 bytes, where size of vertices should be 2160 bytes. (10 particles x 6 vertices x size of VertexType (float4, float2, float3).

                int sizeOfVertexType = sizeof(VertexType);
                int totalSizeVertex = sizeOfVertexType * m_vertexCount;
                memset(m_vertices, 0, totalSizeVertex);
                D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
                vertexBufferData.pSysMem = m_vertices;
                vertexBufferData.SysMemPitch = 0;
                vertexBufferData.SysMemSlicePitch = 0;
    
                int sizeOfMVertices = sizeof(m_vertices);
                CD3D11_BUFFER_DESC vertexBufferDesc(
                    totalSizeVertex,            // byteWidth
                    D3D11_BIND_VERTEX_BUFFER,   // bindFlags
                    D3D11_USAGE_DYNAMIC,        // D3D11_USAGE usage = D3D11_USAGE_DEFAULT
                    D3D11_CPU_ACCESS_WRITE,     // cpuaccessFlags
                    0,                          // miscFlags
                    0                           // structureByteStride
                    );
    
                OutputDebugString(L"Create vertex buffer\n");
    
                DX::ThrowIfFailed(
                    m_d3dDevice->CreateBuffer(
                        &vertexBufferDesc,
                        &vertexBufferData,
                        &m_vertexBuffer
                        )
                    );


    Wednesday, November 28, 2012 3:42 PM