Access violation after saving chart

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Access violation after saving chart

Post by JRadke » Tue Feb 14, 2017 2:03 pm

Hello,

after JPEG export of a zoomed chart an access violation occurs, when I try to unzoom the chart.
Here is my code example:

Code: Select all

unit Unit1;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VclTee.TeeGDIPlus, Vcl.StdCtrls,
  Vcl.ExtDlgs, VCLTee.TeEngine, Vcl.ExtCtrls, VCLTee.TeeProcs, VCLTee.Chart,
  VCLTee.Series, Vcl.Imaging.jpeg;

type
  TForm1 = class(TForm)
    Chart1: TChart;
    SavePictureDialog1: TSavePictureDialog;
    ExportBtn: TButton;
    Series1: TFastLineSeries;
    procedure FormCreate(Sender: TObject);
    procedure ExportBtnClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

(*___________________________________________________________________________*)

function GetChartJPEG(AChart:TCustomChart): TJPEGImage;
var tmpBitmap:TBitmap;
begin
  Result:=TJPEGImage.Create;   { <-- create a TJPEGImage }
  tmpBitmap:=TBitmap.Create;   { <-- create a temporary TBitmap }
  try
    tmpBitmap.Width :=AChart.Width;   { <-- set the bitmap dimensions }
    tmpBitmap.Height:=AChart.Height;
    { draw the Chart on the temporary Bitmap... }
    AChart.Draw(tmpBitmap.Canvas,Rect(0,0,tmpBitmap.Width,tmpBitmap.Height));
    { set the desired JPEG options... }
    With Result do
    begin
      GrayScale            := False;
      ProgressiveEncoding  := True;
      CompressionQuality   := 100;  // % 0 - 100
      PixelFormat          := jf24bit;  // or jf8bit
      ProgressiveDisplay   := True;
      Performance          := jpBestQuality;  // or jpBestSpeed
      Scale                := jsFullSize;  // or jsHalf, jsQuarter, jsEighth
      Smoothing            := false;
      { Copy the temporary Bitmap onto the JPEG image... }
      Assign(tmpBitmap);
    end;
  finally
    tmpBitmap.Free;  { <-- free the temporary Bitmap }
  end;
end;

procedure TForm1.ExportBtnClick(Sender: TObject);
var
  FName: string;
begin
  SavePictureDialog1.Filter := 'JPG-Datei (*.jpg)|*.jpg';
  if not SavePictureDialog1.Execute then Exit;
  FName := SavePictureDialog1.FileName;
  if ExtractFileExt(FName) = '' then FName := FName + '.jpg';

  with GetChartJPEG(Chart1) do begin
    SaveToFile(FName);    { <-- save the JPEG to disk }
    Free;  { <-- free the temporary JPEG object }
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(500);
end;

end.
It seems it makes a difference if the chart is zoomed or not before exporting the image.

Has anybody an idea why it happens?

Thanks
Juergen

Yeray
Site Admin
Site Admin
Posts: 9514
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Access violation after saving chart

Post by Yeray » Wed Feb 15, 2017 7:49 am

Hello Juergen,

I haven't been able to reproduce the problem with TeeChart Pro v2016.19 and RAD XE or RAD 10.1 Berlin Update 2.
What TeeChart version, IDE and OS version are you using?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Re: Access violation after saving chart

Post by JRadke » Wed Feb 15, 2017 1:31 pm

Hello Yeray,

I'm using TeeChart Pro v2016.19 and RAD 10.1 Berlin Update 1.
Meanwhile I found out, that the problem does not happen every time.
Sometimes when I start the program I can zoom the chart, then export the image, then unzoom many times without problems, but at any time it happens anyhow.
Some other time the problem occurs immediately after the first attempt.
It happens on other computer too.
I tried to find out some special conditions to produce the error - without success.
When I start the program in the debugger the error occurs in Vcl.Graphics in the procedure TCustomCanvas.Lock.

To demonstrate the error I have made a screen copy, available at https://goo.gl/photos/ZbaVPc1o82w627ZAA

Regards, Juergen.

Yeray
Site Admin
Site Admin
Posts: 9514
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Access violation after saving chart

Post by Yeray » Thu Feb 16, 2017 7:20 am

Hello Juergen,
JRadke wrote: I'm using TeeChart Pro v2016.19 and RAD 10.1 Berlin Update 1.
We have Update 2 installed on all the machines here. Do you have access to a machine with Update 2 installed to check if it makes any difference?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Re: Access violation after saving chart

Post by JRadke » Thu Feb 16, 2017 9:49 am

Hello Yeray,
sorry, indeed I have Update 2 installed on two machines. Strange to say on one machine Update 1 is displayed when I open Help | Info about, allthough the start displays says Update 2.
These are the complete version numbers on both machines:
Delphi 10.1 Berlin Version 24.0.25048.9432
TeeChart Pro v2016.19.161025 32bit VCL
Both machines are running with Windows 10 Pro 64Bit.
The error occurs on both machines.

Regards
Juergen

Yeray
Site Admin
Site Admin
Posts: 9514
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Access violation after saving chart

Post by Yeray » Thu Feb 16, 2017 1:58 pm

Hello Juergen,

I see the chart in your video is in 2D and has a gradient in the legend and in the back wall, but these properties may be defined in your dfm because they aren't in the .pas you pasted in the opening post.
There may be some property you've set at design-time that could be relevant here, because I still can't reproduce the problem here.

So, could you please attach the test project so we can reproduce the problem here?
Thanks in advance.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Re: Access violation after saving chart

Post by JRadke » Thu Feb 16, 2017 2:29 pm

Hi Yeray,

please find the complete test project here:
https://drive.google.com/file/d/0Bw_A3a ... sp=sharing

Regards
Juergen

Yeray
Site Admin
Site Admin
Posts: 9514
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Access violation after saving chart

Post by Yeray » Thu Feb 16, 2017 3:57 pm

Hello Juergen,

I could reproduce the problem with v2016.19 but it seems to work fine with the current sources we have here.
Since the compiled packages for Berlin are quite big (about 1.5Gb), if you are only interested on win32 I can send you a prerelease version of those binaries. If you need another personality I'm afraid you should wait until we publish the next maintenance release (expected at the beginning March).
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Re: Access violation after saving chart

Post by JRadke » Fri Feb 17, 2017 6:39 am

Hello Yeray,
I'm a little relieved that you could reproduce the problem.
There is no hurry for an interim solution, so I'll wait for your next release in March.

Thank you.
Juergen

JRadke
Newbie
Newbie
Posts: 10
Joined: Mon Oct 31, 2016 12:00 am

Re: Access violation after saving chart

Post by JRadke » Fri Apr 07, 2017 4:03 pm

Hi Yeray,

today I installed TeeChart VCL/FMX RELEASE 2017.21.170329 and hoped to solve the problem, but it sn't.
Please can you explain?

Regards
Juergen

Yeray
Site Admin
Site Admin
Posts: 9514
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Access violation after saving chart

Post by Yeray » Mon Apr 10, 2017 7:46 am

Hello,

I've run the test application you sent here several times using TChart v2017.21. One time it crashed but not the rest of the times.
So it seems I can't reproduce the problem in a consistent basis.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Marc
Site Admin
Site Admin
Posts: 1209
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Access violation after saving chart

Post by Marc » Wed Dec 13, 2017 12:58 pm

Hello Juergen,

I have been able to reproduce the problem with some regularity.

It seems to relate to a problem with the Canvas handle after it has been used for JPEG creation and then 'use returned to' the Chart for conventional canvas use, the problem aggravated when XOr is-being/has-been used to plot zoom rectangle.

This code resolves the problem in initial tests:

Code: Select all

procedure TForm1.ExportBtnClick(Sender: TObject);
var
  FName: string;
  OldCanvas : TCanvas;
begin
  OldCanvas := Chart1.Canvas.ReferenceCanvas;

  SavePictureDialog1.Filter := 'JPG-Datei (*.jpg)|*.jpg';
  if not SavePictureDialog1.Execute then Exit;
  FName := SavePictureDialog1.FileName;
  if ExtractFileExt(FName) = '' then FName := FName + '.jpg';

  with GetChartJPEG(Chart1) do begin
    SaveToFile(FName);    { <-- save the JPEG to disk }
    Free;  { <-- free the temporary JPEG object }
  end;

  Chart1.Canvas.ReferenceCanvas := OldCanvas;
end;
I'm not sure whether we can build the fix into the Chart itself (some kind of check) as the check itself on the Canvas.Handle could AV due to the invalid allocation. The code lines above are seem resilient so far.

We'll check whether there might be other, negative side effects if we build the modification into TeeChart's own export code.

Regards,
Marc Meumann
Steema Support

Marc
Site Admin
Site Admin
Posts: 1209
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Access violation after saving chart

Post by Marc » Wed Dec 13, 2017 3:47 pm

Hi,

as a footnote, TeeChart's own save routine appears to work correctly under similar circumstances:

eg. uses TeeJPEG, TeExport

Code: Select all

TeeSavePanel(TJPEGExportFormat,AChart);
Regards,
Marc
Steema Support

Post Reply