Re: Access violation in TJPEGImage on W2K and XP



jodleren wrote:
      try
        BitmapImage := TBitmap.Create;
        JPEGImage := TJPEGImage.Create;
        if FileExists(sFilename) then
          JPEGImage.LoadFromFile(sFilename)
        else
          JPEGImage.LoadFromFile(WindowsDir + sFilename);
        BitmapImage.Assign(JPEGImage);
        BitmapImage.Dormant;
        BitmapImage.FreeImage;
        BitmapImage.SaveToFile(WindowsDir + sWinImgFile);
        BitmapImage.ReleaseHandle;
        BitmapImage.Free;
        JPEGImage.Free;
      except
        BitmapImage.Free;
        JPEGImage.Free;
      end;

Ugh. Why are you doing so many weird and wonderful calls on your bitmapimage before you save it? Your code could try to free your bitmaps/jpegs more than once. Try this:


  BitmapImage := TBitmap.Create;
  JPEGImage := TJPEGImage.Create;
  try
    if FileExists(sFilename) then
      JPEGImage.LoadFromFile(sFilename)
    else
      JPEGImage.LoadFromFile(WindowsDir + sFilename);
    BitmapImage.Assign(JPEGImage);
    BitmapImage.SaveToFile(WindowsDir + sWinImgFile);
  finally
    BitmapImage.Free;
    JPEGImage.Free;
  end;

You never need to touch BitmapImage/etc again, and certainly do not make them global.

Cheers,
Nicholas Sherlock
.