Solved

How to clear the memory  out side the dll.

Posted on 2002-06-28
6
169 Views
Last Modified: 2010-04-04
Hi,

I am using the heap memory in dll and after usage of that i want to clear that memory. But when i try to use freemem or dispose procedures in the apllication, it is raising the errors. Please comment on this where i made mistake.

And this is sample code
 procedure parseMethod(aInputStream:PChar; aFormattedTextSize:longint; var aParsedText:PChar; var aErrorNumber:Integer);stdcall;external 'MyParse.Dll' ;


//external application sample code
procedure TForm1.btnSaveClick(Sender: TObject);
begin
     parseMethod(tempBfr, intsize, aOutPutStr, aOutPutError);
     ShowMessage( aOutPutStr);
     FreeMem(aOutPutStr,strLen(aOutputStr));


end;



//dll code


procedure parseMethod( inBuffer:PChar; strLength:LongInt; var outBuffer:PChar; var enoint:Integer);cdecl;
var
objParser:TParsePDF;
begin
         
    try
                 objParser:=TParser.Create (inBuffer,strLength); //initialise the parser

                 begin
                         objParser.Parse (outBuffer,enoint); //parse the data
// ***********   here outBuffer Memory will be allocated in Parse Procedure   ******** //
                         enoint:=0;
                 end
           except
           On error: exception do
           begin
                          if strlen(outBuffer)> 0 then
                          begin
                                  enoint:=0;
                          end
                          else
                          begin

                                  strcopy(outBuffer,''); //false document
                                  enoint:=-1;           // set error flag
                          end;
           end;

         end;


end;



Thanks
Koundinya
0
Comment
Question by:sudhakar_koundinya
  • 3
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 7115756
Do you have ShareMem in the uses of both the application and the DLL (as first unit)?
If not then the app and the DLL are using separate heap managers and trying to free memory from one manager with the other manager causes memory corruption.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7115758
sudhakar_koundinya,

SIMPLE:
--------------
Type (in DLL and App):
  uses ShareMem, .... in your uses clause before anything else.


DETAILED:
---------------

If you do not use ShareMem or build all modules with run-time packages each module will have its own instance of the RTL memory manager. Therefore, DLL and your app have  separate MMs, and each one is operating on different memory pool. Consequently, allocating mem in DLL and freeing it in App produces error.

So, you should use ShareMem as a first unit in DLL and App.
Go to Delphi help index and type 'ShareMem', for further info.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7115759
oops,

robert was a bit faster... :)
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7117880
Yes Robert and Cynna,

I did the same. But you are saying that ShareMem should be first unit to be included .If this is the mistake i made, I will check that.

But I have one doubt, why it should be the first unit to be included in both applications?

Thanks
Koundinya
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7118319
sudhakar_koundinya,

> But I have one doubt, why it should be the first unit to be included in both applications?

ShareMem contains an initialization section that installs the memory manager, and we want that to occur before any memory is allocated from any other unit, right?


Also, use it as a first unit in your APP, that is, change your .dpr file like this:

program Project1;

uses
  ShareMem, // <---- !!! (only here, not in Unit1, Unit2, ...)
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

  (.. etc ..)

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7129726
Thanks robert :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 2 69
Why can't I create any project with my delphi EXE file? 6 39
Tviruailstringtree sort multi columns on header click 1 74
shape, triangle, dbctrlgrid 3 17
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question