Strange graphical effects, and memory leaks. Help!

This is a discussion on Strange graphical effects, and memory leaks. Help! within the Windows Programming forums, part of the Platform Specific Boards category; As a beginner of C++, I have made a Windows application that fails. You control a line with w, a, ...

Strange graphical effects, and memory leaks. Help!

As a beginner of C++, I have made a Windows application that fails. You control a line with w, a, s, and d respectively, however the line flashes and sometimes makes permanent paint on the window.

A couple things to note:
My comments are just code snippets with handy functions and whatnot.
Resizing the window will reset the strangely permanent paint.
The application must have memory leaks because when checking a process manage you can see memory usage rising infinitely.
Because of these memory leaks, the line slows down in movement speed, therefore the entire program is slowing down.

Could I ask for someone to help me seal the memory leaks and provide some tips on how to manage window painting better?

Any idea why it flickers? Should I have a system of ticks to only reset on a tick?
I suppose I am referring to frames per second, in which time must be accounted for.

The flickering is caused by clearing the window to the background color on every frame. You see GDI drawing events as they happen -- there is no double buffering. The solution is to blast the entire updated window to the screen in a single piece. To do this, create a memory DC which is compatible with the window DC, draw onto that, then transfer the whole thing in one fell swoop using BitBlt().

Also, your call to UpdateWindow() is redundant, since you are already calling InvalidateRect(). InvalidateRect() will cause WIndows to generate a WM_PAINT automatically (although it does wait until the message queue is empty).

EDIT: As far as frames per second, this can be a factor, but I suspect your flicker is due to the lack of double buffering more so than frame tearing.

I have posted code recently for buffering paint and how this should be done.

To stop the flickering;

try handling WM_ERASEBKGND msgs

and don't ask for the background to be erased so frequently, let the OS handle that.

Code:

InvalidateRect(hWnd , &background, false);

EDIT:
InvalidateRect() //send the app a WM_PAINT msg
UpdateWindow() // post the WM_PAINT directly to the window's callback, not to the OS queue.

Your code is calling for another WM_PAINT while processing a WM_PAINT msg, which then is calling for another WM_PAINT while processing a WM_PAINT msg, which then is calling for another WM_PAINT while processing a WM_PAINT msg, which then is calling for another WM_PAINT while processing a WM_PAINT msg, which then is calling for another WM_PAINT while processing a WM_PAINT msg etc.....