How to free TOleStream in this bit of code
Asked Answered
E

1

6

This is from a custom namespacer handler done in Delphi I use to load files into a webbrowser component.


Datastream:IStream;

var
  F: TFileStream;
  Dummy: INT64;
begin

  F:=TFileStream.Create(strfilename fmOpenRead);
  CreateStreamOnHGlobal(0, True, DataStream);
  TOleStream.Create(DataStream).CopyFrom(F, F.Size);
  DataStream.Seek(0, STREAM_SEEK_SET, Dummy);
  TotalSize := F.Size;
  F.Free;
end;

The problem is that Fastmm4 gives a memory leak error when the program ends and says that TOleStream was not freed. How do I free it? If I put TOleStream in a variable and assign like this

var
TOS:TOleStream;

TOS:=TOleStream.Create(DataStream)
TOS.CopyFrom(F, F.Size);

..
TOS.Free;
End;

I get an error (EAccessViolation pointing to TComObject.ObjRelease in ComObj)in the end when I free the TOS variable. I would appreciate your comments and help. This problem has been bugging me for a while now.

I am also wondering if it could be something with FastMM4? Could it be giving false information? In a bit of code like the following. How is TOLEStream freed typically?

f.SaveToStream(TOleStream.Create(DataStream));

And is there a way to send data to datastream without using the TFileStream?

Hi Remy, You can find a working demo project of the namespace handler with the fastmm and the problem procedure here: http://mode5.webs.com/namespace.7z When the project loads, click the button to have the namespace handler load the files. When you exit, you should get the fastmm error. Kind regards,

Editor answered 14/12, 2010 at 19:35 Comment(2)
Hi, Mode, welcome to StackOverflow. That's a pretty good question, but it would help if we knew what the error you get when you free it is. Can you edit your question and copy the error you get into here for us? Thanks.Kazukokb
FastMM is not wrong. What are you exactly trying to do? Load a file into an IStream and do something with it? Should the file be modified or do you want it to be readonly?Drew
B
4

If you need to pass an IStream interface to something, you should look into the TStreamAdapter object, you can create one on the TFileStream, and pass its IStream interface.

Burch answered 15/12, 2010 at 7:39 Comment(4)
Thanks Stijn, I am going to try thatEditor
Thanks Stijn, Can you elaborate a little bit? When I try using Datastream:=TStreamadapter.create(f,soOwned); Fastmm4 throws an error saying I am trying to use an interface of a freed object. Some demo code would be helpful. Thanks again.Editor
I'm not sure I understand what you mean by "custom namespacer handler done in Delphi I use to load files into a webbrowser component" I don't think a custom namespace hander is the best way to load webbrowser content. There's IInternetProtocol, but that's a lot of work to implement yourself, I know because I tried here: xxm.sf.netBurch
@Mode: please show your actual code that you are having trouble with.Iphagenia

© 2022 - 2024 — McMap. All rights reserved.