Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Error when Freeing a TJpegImage



Permlink Replies: 5 - Last Post: Dec 2, 2017 4:49 AM Last Post By: Adalberto Baldini Threads: [ Previous | Next ]
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2017 5:17 AM
Delphi XE5

Jpeg_Compressed is a TJpegImage local var I create inside a function.
try
Jpeg_Compressed := TJPEGImage.Create;
Jpeg := TJPEGImage.Create;..
..........
try
frm_Main.Image_Field.Picture.Assign(Jpeg_Compressed);
frm_Main.Lbl_Dim_IO.Caption := Lbl_Dimensione.Caption ;
blob := Frm_Main.IBDts_Fields.CreateBlobStream(
Frm_Main.IBDts_Fields.FieldByName('FOTO'), bmWrite);
blob.Seek(0, soFromBeginning);
Jpeg_Compressed.SaveToStream(blob); // scrive il campo nel Dataset !!!!

finally
blob.Free ;
end;
finally
Jpeg.Free;
Jpeg_Compressed.Free;
end;

when execute : Jpeg_Compressed.Free;

I get error message : 'Invalid Pointer Operation'

What suggestion on manage it ?

Regards
Adalberto Baldini
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2017 6:21 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

Delphi XE5

Jpeg_Compressed is a TJpegImage local var I create inside a function.
try
Jpeg_Compressed := TJPEGImage.Create;
Jpeg := TJPEGImage.Create;..
..........
try
frm_Main.Image_Field.Picture.Assign(Jpeg_Compressed);
frm_Main.Lbl_Dim_IO.Caption := Lbl_Dimensione.Caption ;
blob := Frm_Main.IBDts_Fields.CreateBlobStream(
Frm_Main.IBDts_Fields.FieldByName('FOTO'), bmWrite);
blob.Seek(0, soFromBeginning);
Jpeg_Compressed.SaveToStream(blob); // scrive il campo nel
Dataset !!!!

finally
blob.Free ;
end;
finally
Jpeg.Free;
Jpeg_Compressed.Free;
end;

when execute : Jpeg_Compressed.Free;

I get error message : 'Invalid Pointer Operation'

What suggestion on manage it ?

Regards
Adalberto Baldini

You should post the complete code here. Most probably in your code Jpeg
and Jpeg_Compressed are pointing to the same object and you just try to
free it twice.
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Re: Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2017 7:57 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
You should post the complete code here. Most probably in your code Jpeg
and Jpeg_Compressed are pointing to the same object and you just try to
free it twice.

procedure TImageBox.SpdBtn_SaveClick(Sender: TObject);
var
blob : TStream;
Mem_Stream : TMemoryStream;
Jpeg, Jpeg_Compressed: TJPEGImage;
rate, dimension, size : int64;
begin
// executed only with IBDts_Fiels in Insert/Edit
try
Jpeg := TJPEGImage.Create;
Jpeg_Compressed := TJPEGImage.Create;
Jpeg.Assign(Shared_Image.Picture);
Jpeg_Compressed.Assign(Shared_Image.Picture);
// comprime immagine
if not(wRdGrp_Filesize.ItemIndex = 2) then
begin
if wRdGrp_Filesize.ItemIndex = 0 then rate := 500000
else if wRdGrp_Filesize.ItemIndex = 1 then rate := 1000000;
size := Image_Size(Jpeg);
if size > rate then
begin
Jpeg_Compressed := Compress_Image(Jpeg, rate, size);
size := Image_Size(Jpeg_Compressed);
end ;
Lbl_Dimensione.Caption := Format('%.0n', [size + 0.0]);
end;
try
frm_Main.Image_Field.Picture.Assign(Jpeg_Compressed);
frm_Main.Lbl_Dim_IO.Caption := Lbl_Dimensione.Caption ;
blob := Frm_Main.IBDts_Fields.CreateBlobStream(
Frm_Main.IBDts_Fields.FieldByName('FOTO'), bmWrite);
blob.Seek(0, soFromBeginning);
Jpeg_Compressed.SaveToStream(blob); // scrive il campo nel Dataset !!!!

finally
blob.Free ;
end;
finally
Jpeg.Free;
try
Jpeg_Compressed.Free;
except
//
end;
end;
Self.Visible := False;
Frm_Main.SetFocus;
end;
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2017 8:28 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

You should post the complete code here. Most probably in your code
Jpeg and Jpeg_Compressed are pointing to the same object and you
just try to free it twice.

procedure TImageBox.SpdBtn_SaveClick(Sender: TObject);
var
blob : TStream;
Mem_Stream : TMemoryStream;
Jpeg, Jpeg_Compressed: TJPEGImage;
rate, dimension, size : int64;
begin
// executed only with IBDts_Fiels in Insert/Edit
try
Jpeg := TJPEGImage.Create;
Jpeg_Compressed := TJPEGImage.Create;
Jpeg.Assign(Shared_Image.Picture);
Jpeg_Compressed.Assign(Shared_Image.Picture);
// comprime immagine
if not(wRdGrp_Filesize.ItemIndex = 2) then
begin
if wRdGrp_Filesize.ItemIndex = 0 then rate := 500000
else if wRdGrp_Filesize.ItemIndex = 1 then rate := 1000000;
size := Image_Size(Jpeg);
if size > rate then
begin
Jpeg_Compressed := Compress_Image(Jpeg, rate, size);
size := Image_Size(Jpeg_Compressed);
end ;
Lbl_Dimensione.Caption := Format('%.0n', [size + 0.0]);
end;
try
frm_Main.Image_Field.Picture.Assign(Jpeg_Compressed);
frm_Main.Lbl_Dim_IO.Caption := Lbl_Dimensione.Caption ;
blob := Frm_Main.IBDts_Fields.CreateBlobStream(
Frm_Main.IBDts_Fields.FieldByName('FOTO'), bmWrite);
blob.Seek(0, soFromBeginning);
Jpeg_Compressed.SaveToStream(blob); // scrive il campo nel
Dataset !!!!

finally
blob.Free ;
end;
finally
Jpeg.Free;
try
Jpeg_Compressed.Free;
except
//
end;
end;
Self.Visible := False;
Frm_Main.SetFocus;
end;

Most probably the problem is the Compress_image function from your
other post, after that function both Jpeg_compressed and jpeg will
reference the same object instance and you cannot free an object twice
as you're doing now.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2017 11:05 AM   in response to: Adalberto Baldini in response to: Adalberto Baldini
Adalberto Baldini wrote:

procedure TImageBox.SpdBtn_SaveClick(Sender: TObject);

Based on the Compress_Image() code you showed earlier, you are leaking
your allocated Jpeg_Compressed object and reassigning that pointer to
point at your Jpeg object instead. Then you call Free() on both
pointers, thus trying to free the same object twice, causing the crash.

You don't need the Jpeg_Compressed object at all. Try this instead:

procedure TImageBox.SpdBtn_SaveClick(Sender: TObject);
var
  blob : TStream;
  Jpeg: TJPEGImage;
  rate, dimension, size : int64;
begin
  // executed only with IBDts_Fiels in Insert/Edit
  Jpeg := TJPEGImage.Create;
  try
    Jpeg.Assign(Shared_Image.Picture);
    // comprime immagine
    if wRdGrp_Filesize.ItemIndex <> 2 then
    begin
      if wRdGrp_Filesize.ItemIndex = 0 then
        rate := 500000
      else {if wRdGrp_Filesize.ItemIndex = 1 then}
        rate := 1000000;
      size := Image_Size(Jpeg);
      if size > rate then
      begin
        Compress_Image(Jpeg, rate, size);
        size := Image_Size(Jpeg);
      end;
      Lbl_Dimensione.Caption :=  Format('%.0n', [size + 0.0]);
    end;
    frm_Main.Image_Field.Picture.Assign(Jpeg);
    frm_Main.Lbl_Dim_IO.Caption := Lbl_Dimensione.Caption;
    blob :=
Frm_Main.IBDts_Fields.CreateBlobStream(Frm_Main.IBDts_Fields.FieldByName
('FOTO'), bmWrite);
    try
      Jpeg.SaveToStream(blob); // scrive il campo nel Dataset !!!!
    finally
      blob.Free;
    end;
  finally
    Jpeg.Free;
  end;
  Self.Visible := False;
  Frm_Main.SetFocus;
end;


--
Remy Lebeau (TeamB)
Adalberto Baldini

Posts: 139
Registered: 1/31/12
Re: Error when Freeing a TJpegImage
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2017 4:49 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
We discussed many versions of my code, In yr suggestion the code instruction : Compress_Image(Jpeg, rate, size);
refers to a procedure Compress_Image(var Jpeg_wk : TJpegImage; rate, size Int64) or what ?

Adalberto
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02