NO

Author Topic: My graphic program crashes  (Read 6376 times)

allan.emren

  • Guest
My graphic program crashes
« on: August 02, 2010, 12:12:02 PM »
Hello,
I am developing a GPS-synthetic vision program for pocket PC.
At the present experimental stage, the landscape is flat, with just a few objects in 3D.

Once a second, the screen is erased and a new picture is drawn.

My problem is that after some 2000 - 6000 seconds, the graphics stops functioning. The picture becomes black-white, and clearing the screen stops functioning.

So using the program in flights, I have to stop the program every half an hour and start it again.

Anyone has an idea on how to fix the problem?

My first thought was that some variable dimension was too small. This has turned out not to be the case.

So now, I guess that some resource is not properly deleted.

One (less good) solution would be to automatically quit (or reset)  the program and restart it at regular intervals. Quitting is easy enough, but how can I make it start again automatically?

In the lines below, I will describe the most important functions used for drawing.

The program draws in the main window. Before drawing anything (first time), I call the function

void Open_screen( HDC *win, HWND hwndDlg )
{
   RECT bit_map;

   Textposx = 1;
   Textposy = 28;
   Textlength = 10;
   Textsize = 10;
   Window = win;
   BM_window = hwndDlg;
   GetClientRect( BM_window, &bit_map);
   Winheight = bit_map.bottom + 1;
   Winwidth = bit_map.right + 1;
   Pencil = CreatePen(PS_SOLID, 2, RGB( 0, 0, 0 ) ); // BLACK
   Eraser = CreateSolidBrush( 0x00FFFFFF );           // WHITE
   Paintbrush = CreateSolidBrush( 0x00000000 );      // BLACK
   SelectObject(*Window, Pencil);
   SetROP2(*Window, R2_MASKPEN);
}

To select the current color, I use the function Set_color. Is anything missing in that function?

void Set_color( int red, int green, int blue )
{
   DeleteObject( Pencil );
   DeleteObject( Paintbrush );
   Pencil = CreatePen( PS_SOLID, 2, RGB( red, green, blue ) );
   Paintbrush = CreateSolidBrush( Rgb( red, green, blue ) );
   SelectObject(*Window, Pencil);
   PixColor.red = red;
   PixColor.green = green;
   PixColor.blue = blue;
   SetROP2( *Window, R2_COPYPEN );
}

Lines are drawn by the functions Move_to and Line_to

void Move_to( float x, float y)
{
   X_win = Transform_x( x );
   Y_win = Transform_y( y );
   MoveToEx( *Window, X_win, Y_win, (LPPOINT) NULL);
   Textposx = X_win;
   Textposy = Y_win;
}

void Line_to( float x, float y)
{
   X_win = Transform_x( x );
   Y_win = Transform_y( y );

   LineTo( *Window, X_win, Y_win );
}

The Transform functions make transformations from real world coordinates to screen coordinates, X_win and Y_win, that are integers.

Text is written by the function

void Prints( WCHAR *text )
{
   Mkstring( text );
   ExtTextOut( *Window, Textposx, Textposy, 0, NULL, String, Textlength, NULL);
   Textposy += 12;
}

The Move_to function sets text position as well, so text comes at the object described.

Drawing sky and ground (and erasing previous picture) is performed by the following lines

   Set_color( SKYBLUE );
   FillRect( windDC, &Sky, Paintbrush );      // Paint sky
   Set_color( LIGHTGREEN );
   FillRect( windDC, &Ground, Paintbrush );   // Paint ground


Trying to solve the problem, I have made a simulated version for PC. There is no GPS-signal involved. It is simulated as well. The program behaves similarly there. The program crash does not occur at any specified time, neither in PC or pocket-PC, but typically within the time interval mentioned above.

Anyone has a solution to the problem?

Allan


Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: My graphic program crashes
« Reply #1 on: August 02, 2010, 12:26:10 PM »
Have you checked the memory usage, since it could be a memory leak.
---
Stefan

Proud member of the UltraDefrag Development Team

allan.emren

  • Guest
Re: My graphic program crashes
« Reply #2 on: August 02, 2010, 09:28:09 PM »
Thank you Stefan,
I have considered this. The program does not handle memory except for usual variables, some of them being structures. To make sure that the problem is not caused by defective de-declaration of auto variables, I tried making all variables static (or global). This had no effect.

Of course, some of the windows functions also allocates memory, and I suspect that it is not released properly.

Do you possibly know of a method to make a total program reset from within the program?

Allan

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: My graphic program crashes
« Reply #3 on: August 03, 2010, 10:45:36 AM »
I would use the Sysinternals Process Explorer to monitor the resources used by your program.

You can monitor not only memory usage, but used handles as well.

You should be able track down the problematic area based on the monitoring.

Sorry for being not much help in the GDI area.
---
Stefan

Proud member of the UltraDefrag Development Team

Offline DMac

  • Member
  • *
  • Posts: 272
Re: My graphic program crashes
« Reply #4 on: August 03, 2010, 08:19:25 PM »
Hello Allen,

How often do you call Open_screen()?  If once and only once then almost ok.

One thing that you need to do is free the object that you are replacing in a device context.

Code: [Select]
SelectObject(*Window, Pencil);
Should be

Code: [Select]
DeleteObject(SelectObject(*Window, Pencil));
This also applies to the Set_color() method.
No one cares how much you know,
until they know how much you care.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: My graphic program crashes
« Reply #5 on: August 07, 2010, 02:03:10 PM »
As a follow up, see the Pushing the Limits of Windows series at Mark Russinovich's blog.

There is a limit of GDI and USER objects and if you exceed those limits your application will act strange.
---
Stefan

Proud member of the UltraDefrag Development Team

allan.emren

  • Guest
Re: My graphic program crashes
« Reply #6 on: April 11, 2011, 01:51:18 PM »
Thanks DMac and Stefan :)
The problem is now solved. The statement Open_screen() was intended to be used only once.
It was, however, positioned wrongly, so that it was ececuted every four seconds.
This, of course caused a huge number of open objects.

Cheers,

Allan