Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-06

Can't close and reopen ptcgraph

7 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I ran into an unexpected issue with ptcgraph. If I use closegraph, I cannot re-open a new ptcgraph window with Initgraph. It looks like it opens the second graph window but then closes it immediately after.. my program is then appears to be locked up after this happens.. no errors, just locked up. test program is below. If I comment out ptcgraph and ptccrt and uncomment graph and wincrt, it works as expected.

James

program ReOpenGraph.pas;
{$mode objfpc}{$H+}

uses

  ptcgraph,ptccrt;
//  graph,wincrt;
var
  gd,gm : smallint;
begin
  gd:=d8Bit;
  gm:=m800x600;
  Writeln('Opening Graphics Window');
  Initgraph(gd,gm,'');
  Writeln('Graphics Window Open');
  setcolor($4A);
  settextstyle(Defaultfont,HORIZDIR,2);
  outtextxy(30,30,'Graphics Window Open');
  readkey;
  Closegraph;
  Writeln('Closing Graphics Window');
  readln;
  Writeln('Re-Opening Graphics Window');
  Initgraph(gd,gm,''); // window opens then closes immediately with ptcgraph
  Writeln('Graphics Window Open Again'); // this never happens with ptcgraph
  setcolor($4A);
  settextstyle(Defaultfont,HORIZDIR,2);
  outtextxy(30,30,'Graphics Window Open Again');
  Readln;
  Closegraph;
end.

Reply by email

Stefan V. Pantazi
to James Richters

On Linux 64bit, reopening of a second window works fine, as expected. On Windows it does not, and behaves as you reported. This is most likely related to the use of a separate thread (TPTCWrapperThread) in ptcgraph. As far as I know graph and wincrt do not use threads. I suspect that on windows, the ptc thread is not properly terminating for some reason.

Stefan

James Richters wrote 7 lines
I ran into an unexpected issue with ptcgraph. If I use closegraph, I cannot re-open a new ptcgraph window with Initgraph. It looks like it opens the second graph window but then closes it immediately after.. my program is then appears to be locked up after this happens.. no errors, just locked up. test program is below. If I comment out ptcgraph and ptccrt and uncomment graph and wincrt, it works as expected.

Reply by email

Stefan V. Pantazi
to James Richters: I ran into an unexpected issue with ptcgraph. If I use closegraph, I cannot re-open a…

I think I figured it out. It hink ptcgraph was not designed with that functionality in mind. If you take a look at the ptcgraph unit, it has initialization and finalization sections where the ptc wrapper object is created and freed. That happens only at application start and finish not during run, as you require.

As a workaround, you can manually take care of the ptcwrapper object like this, in between closegraph and initgraph calls, but first ask yourself, do you really need this?

[...]
//close graph window
ptcgraph.Closegraph;
//manually terminate the ptcwrapper thread and free object
PTCWrapperObject.Terminate;
PTCWrapperObject.WaitFor;
PTCWrapperObject.Free;
//manually create the ptcwrapper thread
PTCWrapperObject := TPTCWrapperThread.Create;
//init graph windows as usual
ptcgraph.Initgraph(gd,gm,'');
[...]

Stefan

James Richters wrote 6 lines
I ran into an unexpected issue with ptcgraph. If I use closegraph, I cannot re-open a new ptcgraph window with Initgraph. It looks like it opens the second graph window but then closes it immediately after.. my program is then appears to be locked up after this happens.. no errors, just locked up. test program is below. If I comment out ptcgraph and ptccrt and uncomment graph and wincrt, it works as expected.

Reply by email

James Richters
to Stefan V. Pantazi
As a workaround, you can manually take care of the ptcwrapper object like this, in between closegraph and initgraph calls, but first ask yourself, do you really need this?
Thanks for figuring out what is going on.  I don't really need to close it and reopen it. I can work around by just hiding the window with
ShowWindow(graphwindow,SW_hide);
To get back to my console window, then when needed I can clear it and show it again with.
ShowWindow(graphwindow,SW_show);

James

Reply by email

James Richters
to Stefan V. Pantazi: I think I figured it out. It hink ptcgraph was not designed with that functionality in…

I thought I would try to get my demo program to work. When I try to compile it with

//manually create the ptcwrapper thread
PTCWrapperObject := TPTCWrapperThread.Create;
I get reopengraph.pas(32,23) Error: Identifier not found "TPTCWrapperThread"

Demo program below... Any ideas?

James

{$mode objfpc}{$H+}
program ReOpenGraph.pas;
uses
  ptcgraph,ptccrt;
var
  gd,gm : smallint;
begin
  gd:=d8Bit;
  gm:=m800x600;
  Writeln('Opening Graphics Window');
  Initgraph(gd,gm,'');
  Writeln('Graphics Window Open');
  setcolor($4A);
  settextstyle(Defaultfont,HORIZDIR,2);
  outtextxy(30,30,'Graphics Window Open');
  readkey;
  //close graph window
  Closegraph;
  //manually terminate the ptcwrapper thread and free object
  PTCWrapperObject.Terminate;
  PTCWrapperObject.WaitFor;
  PTCWrapperObject.Free;
  Writeln('Closing Graphics Window');
  readln;
  Writeln('Create ptcwrapper thread');
  //manually create the ptcwrapper thread
  PTCWrapperObject := TPTCWrapperThread.Create; //compiler error
  Writeln('Re-Opening Graphics Window');
  //init graph windows as usual
  Initgraph(gd,gm,''); // window opens then closes immediately with ptcgraph
  Writeln('Graphics Window Open Again'); // this never happens with ptcgraph
  setcolor($4A);
  settextstyle(Defaultfont,HORIZDIR,2);
  outtextxy(30,30,'Graphics Window Open Again');
  Readln;
  Closegraph;
end.

Reply by email