locked
E_OUTOFMEMORY =(

    Question

  • I am trying to draw polylines/polygons. They draw, but they leak memory like crazy. I would never have noticed until i tried drawing an 8x8 grid of rectangles, where it crashes in about 2 seconds. I have no idea why it leaks...Here's the code for the polyline function

    //========================================================================

    // count = sizeof(vertices) / sizeof(Vertex)

    void Graphics::DrawPolyLine(Vertex *vertices, unsigned count)

    {

    HRESULT hr = S_OK;

    hr = pDevice->CreateVertexBuffer(0, 0,

      D3DFVF_COL, D3DPOOL_DEFAULT, &vertexstorage, NULL);

    if (SUCCEEDED(hr))

      hr = pDevice->SetStreamSource(0, vertexstorage, 0, sizeof(Vertex));

    else

    {

      if (hr == D3DERR_INVALIDCALL)

        return;

      if (hr == D3DERR_OUTOFVIDEOMEMORY)

        return;

      if (hr == E_OUTOFMEMORY)

        return;

    }

    if (SUCCEEDED(hr))

      hr = pDevice->SetFVF(D3DFVF_COL);

    if (SUCCEEDED(hr))

      pDevice->SetTexture(0, NULL);

    void *pVertices;

    if (SUCCEEDED(hr))

      hr = vertexstorage->Lock(0, count * sizeof(Vertex), &pVertices, 0);

    if (SUCCEEDED(hr))

      memcpy(pVertices, vertices, count * sizeof(Vertex));

    if (SUCCEEDED(hr))

      hr = vertexstorage->Unlock();

    if (SUCCEEDED(hr))

      hr = pDevice->DrawPrimitive(D3DPT_LINESTRIP, 0, count - 1);

    if (SUCCEEDED(hr))

      hr = pDevice->CreateVertexBuffer(TEXTURE_VERTICES * sizeof(Vector), 0,

        D3DFVF_TEX, D3DPOOL_DEFAULT, &vertexstorage, 0);

    if (SUCCEEDED(hr))

      hr = pDevice->SetStreamSource(0, vertexstorage, 0, sizeof(Vector));

    if (SUCCEEDED(hr))

      hr = pDevice->SetFVF(D3DFVF_TEX);

    //=====================================================================

    Note that i'm recreating the vertex buffer each time because i'm also using a different vertex class for textured objects.

    Thanks for reading this far!

    Saturday, March 24, 2007 10:40 AM

Answers

  • It looks like you're never releasing the D3D vertex buffer interface, at least in the code shown.  Depending on how many times this function is called and the size of the buffers created each time, you can quickly run out of memory.  Call vertexstorage->Release() after you use it the first time (after the Draw call and before creating it again later on) and then call it again after the second created buffer is used.
    Saturday, March 24, 2007 7:25 PM

All replies

  • It looks like you're never releasing the D3D vertex buffer interface, at least in the code shown.  Depending on how many times this function is called and the size of the buffers created each time, you can quickly run out of memory.  Call vertexstorage->Release() after you use it the first time (after the Draw call and before creating it again later on) and then call it again after the second created buffer is used.
    Saturday, March 24, 2007 7:25 PM
  • Thank you very much! Now the only memory leak i have is when i move the mouse...
    Saturday, March 24, 2007 10:47 PM