0

I have a webservice created with RemObjects SDK over Delphi XE and a function published that to do his works needs to use com objects (Specifically Ado connection to database using DMO). I have detected that when I call this function a lot of times the memory that uses the webservice increases dramatically.

The cause of the memory leak is that the memory that is reserved for the com objects is not released never.

What I have to do to fix that?

Thanks

Code:

constructor TA.Create;
begin
    inherited Create;
    oServer := CoSQLServer.Create;
end;

destructor TA.Destroy;
begin
   oServer := nil;
end;

Declaration

class function CoSQLServer.Create: _SQLServer;
begin
    Result := CreateComObject(CLASS_SQLServer) as _SQLServer;
end;
13
  • How are you measuring the memory use? And the solution seems clear - you need to release the COM objects you're not using any longer (or simply don't create them all; create them once and re-use them). Commented Dec 28, 2012 at 17:09
  • Every call on the webservice is an independent thread. How I realase COM Objects? I tried to set the objects to nil and it doesn't work Commented Dec 28, 2012 at 17:12
  • I can't answer that, because there's no code in your question to use to determine the answer. Commented Dec 28, 2012 at 17:26
  • What is CoSQLServer? I know it's a COM object, but what is the COM class it points to? Commented Dec 28, 2012 at 17:31
  • The COM Object of import Microsoft SQLDMO Object Library Commented Dec 28, 2012 at 17:37

1 Answer 1

1

In the code you show, the only thing that we can criticise is your destructor:

destructor TA.Destroy;
begin
  oServer := nil;
end;

Assigning nil to oServer is rather pointless (albeit harmless) since that will happen automatically.

But what is missing here is a call to the inherited destructor. So, your destructor should look like this:

destructor TA.Destroy;
begin
  oServer := nil;
  inherited;
end;

Now, if you decided to remove the finalization of oServer then you could simply remove the destructor from your class and rely on that of the base class.

However, you do appear to be measuring your memory leak with an invalid tool. You can't measure memory leaks with task manager. Use the FastMM leak detector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.