Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with meomory-leaks

Status
Not open for further replies.

Kostarsus

Programmer
Nov 30, 2000
86
DE
Hello,

I'm new to managed DirectX and have a great problem with memory leaks.
I think, its the best when I tell you something about the project and the
structure of the project.

The goal of the project is to render a scenegraph in a control ( I use a
panel as control, because I mentioned, that the picture box is buggy for
this).
Becuase there can be a lot of different objects I have written an own class
for rendering. The class is named RenderMain and is established in an own
assembly with other classes. This class has a property for the device and a
property for the control. The Mainform is established in an other assembly.

I'd overriden the OnPaint-Method of the Mainform like this:
Code:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Application.DoEvents();
    if (m_RenderMain!=null)
        m_RenderMain.PaintDevice();
    this.Invalidate();
}
The Methode m_RenderMain.PaintDevice() looks like this:
Code:
public void PaintDevice()
{
    m_device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
System.Drawing.Color.Black,1f,0);
    m_device.BeginScene();
    m_device.RenderState.Lighting=true;
    m_device.RenderState.CullMode=Cull.None;
    if (m_IsViewpointDefined==false)
        SetupStandardCamera();
    if (m_IsLightDefined==false)
        m_device.RenderState.Ambient=System.Drawing.Color.White;
    ReadScenegraph();
    m_device.EndScene();
    m_device.Present();
}
In the RenderScenegraph I render all items of the scenegraph. For this
purpose, every item of the scenegraph have a method which looks like thius:
Code:
public Device Action(Device directXDevice)
{
    //render item
    return directXdevice;
}
The device is initialised in the following method of the RenderMain class.
Code:
public void InitializeDevice()
{
    PresentParameters myPresentParameters= new PresentParameters();
    myPresentParameters.Windowed=true;
    myPresentParameters.SwapEffect=SwapEffect.Discard;
    myPresentParameters.AutoDepthStencilFormat=DepthFormat.D16;
    myPresentParameters.EnableAutoDepthStencil=true;
    int adapterOrdinal=Manager.Adapters.Default.Adapter;
    CreateFlags flags= CreateFlags.SoftwareVertexProcessing;

    //Check to see, if we can use a pure hardw<redevice
    Caps caps=Manager.GetDeviceCaps(adapterOrdinal,DeviceType.Hardware);
    if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
    {
        //Replace softwarevertex with hardwarevertex
        flags=CreateFlags.HardwareVertexProcessing;
    }
    //Do we support pure device?
    if (caps.DeviceCaps.SupportsPureDevice)
        flags |=CreateFlags.PureDevice;

    m_device=new
Device(adapterOrdinal,DeviceType.Hardware,m_control,flags,myPresentParameter
s);
}
All works fine and the graphical objects are rendered, but if I start the
taskmanager, I see, that the memory-use is increasing by every repaint of
the Mainform.
This is happening, everytime when I'm using the device in any form.
I'd tried to place the methods InitializeDevice and RenderScenegraph in the
Mainform but without any effects.

So my question is, can anyone help me, to solve the problem of increasing
memory-use?

cu Thomas


 
I am not a direct x programmer but I do know that when you are using device contexts in C++ and you select an object into them, you must later select the return value back in. For example

CBitmap* pOldBMP = myDC.SelectObject(&myBitmap);
... do stuff
myDC.SelectObject(pOldBitmap);
// end function

As I said, I dont yet program directX and my post may be way off but it was worth a shot.

Matt
 
I generally use C++ for DirectX stuff, but I've used C# once or twice so I'll see if I can help. First of all, you shouldn't be calling DoEvents() and Invalidate() in your OnPaint method. That should go in a while loop in Main(). Since C# is garbage collected, you can clean up your leak by calling GC.Collect() every now and then, but you shouldn't have to do this. My advice is comment out lines until you find exactly what method is causing this so that we can have a better idea of what's going on. All I could guess is that you are somehow calling InitDevice() more than once.
 
Well, the leak happens ever, when I use the device.
Even if I setup the camera.

I need the DoEvents call, because the application work on a control in a form. If I don't use the DoEvents call, all other components of the form aren't rendered correctly.

cu kostarsus.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top