Subject UDF for reading images into a blob.
Author will.honor
Hello,
I have hit a bit a wall with writing a udf and I wonder if someone
could help me. I recently found some code in this list for reading and
writing file data into blobs in a udf.

http://tech.groups.yahoo.com/group/firebird-support/message/85010

I'm interested in using this code to read an image off disk and
return it as a blob field in a view. The contributed code does work as
far as it will return an image blob. The only problem is it won't
release the memory used until the connection is dropped. I have
adapted the code slightly for my needs and simplified it so that it
returns the same hard coded image every time. It still leaks memory,
What am I doing wrong?

Thanks in advance.
Will.

TGetSegmentProc = function (Handle: Pointer;
var Buffer;
Size: Integer;
var Read: Integer): Integer cdecl;
TPutSegmentProc = function (Handle: Pointer;
var Buffer;
Size: Integer): Integer cdecl;
PBlob = ^TBlob;
TBlob = record
GetSegment: TGetSegmentProc;
Handle: Pointer;
Segments: Integer;
MaxLength: Integer;
TotalSize: Integer;
PutSegment: TPutSegmentProc;
end;

procedure LoadBlobFromFile(Blob: PBlob); cdecl; export;
var
buffer: PByteArray;
FS: TFileStream;
BufferRead,BufLen : integer ;
begin
if not FileExists('D:\Working\Icons\logo\final.bmp') then Exit;
BufLen := 4096 ;
GetMem(Buffer, BufLen);
try
FS := TFileStream.Create('D:\Working\Icons\logo\final.bmp',
fmOpenRead or fmShareDenyWrite);
try
FS.Position := 0 ;
while FS.Position < FS.Size do
begin
BufferRead := FS.Read( buffer^, BufLen) ;
Blob^.PutSegment( Blob^.Handle, buffer^, BufferRead);
end;
finally
FS.Free;
end;
finally
FreeMem(Buffer) ;
end;
end;


DECLARE EXTERNAL FUNCTION LOADBLOBFROMFILE
BLOB
RETURNS PARAMETER 1
ENTRY_POINT 'LoadBlobFromFile' MODULE_NAME 'UDFTest'