Strange AV while loading an icon from the resource stream

I'm adding icons to my project via Resources and Images dialog, and decided to walk away from TIcon.LoadFromResourceName because it does not perform any error checking. I wrote seemingly equivalent code but does not work as expected.

Here is a testcase which fails for me:

procedure TForm1.FormCreate(Sender: TObject);
const
  ResName = 'Icon_1';
var
  Stream: TResourceStream;
begin
  OutputDebugString(PChar(Format('RTLVersion = %f', [RTLVersion])));
  Assert(Assigned(Image1), 'Image1 comes from the designer, should be instantiated already');

  Stream := TResourceStream.Create(HInstance, ResName, RT_GROUP_ICON);
  try
    Image1.Picture.Icon.LoadFromStream(Stream); // AV here
  finally
    Stream.Free;
  end;
end;

A/V occurs deeply inside library at write of address 0x00000008, which suggest some uninitialized object instance.

I'm doing anything wrong or it is a library bug? Library version is 23.0, Delphi XE2

Answers


OK, the solution is to stop confusing RT_GROUP_ICON and RT_ICON. RT_GROUP_ICON contains only icon header and directory (GRPICONDIR and GRPICONDIRENTRY respectively), but actual images and masks are stored separately as RT_ICON resources. Additionally, icon file headers and icon group headers have exactly same layout and magic values, making detection of invalid data stream impossible. Therefore, it is absolutely not possible to use TIcon.LoadFromStream with RT_GROUP_ICON resource, this method has been designed for disk files only.

Thanks to bummi for shedding a light onto this nuance.

Additional reading: LookupIconIdFromDirectoryEx function reference and the corresponding example code.


Need Your Help

OnClick() method doesn't work in TextView

java android android-layout navigation android-textview

I have a textView with OnClick method to navigate another activity. But, If i pressed that text it doesn't navigate. But If i used Button instead TextView, it works perfectly. Can't use OnClick met...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.