Search the Community
Showing results for tags 'olevariant'.
Found 1 result
-
We have an old windows service, recently upgraded in Delphi Rio, that spawns off few threads. Each thread is interacting with COM DLLs. Each thread is working on its own and doesn't need synchronization. Each thread works on a separate record, so no issues of locking. When I run following code, the memory rapidly increases in the Task Manager as threads process records. Its also reported as BSTR (Widestring) type leak in Deleaker. And finally, we end up having the "out of memory". Each thread does following: var a: IBusinessClass; begin a := ObjCOMServer.GetNewInstance('TBusinessClass') // Not the actual call, but the idea is to ask a COM dll to give a new instance of TBusinessClass a.RowId := GetNextId; a.AWideStringProperty := 'Test'; a.AnIntProperty := 1001; ....etc... SaveCOMServer.Save(a.asXML, ...) // This is the prior to upgrade code. If I comment out this line, leak doesn't happen // SaveCOMServer.Save(String(a.asXML), ...) <--- If I execute this statement then it doesn't leak. end; We tried following but still memory increased in the same way above... (1). Declare a local variable of type OleVariant to hold a.asXML..and pass it to Save(). (2). In the Save() on SaveCOMServer, the first statement we put is "exit" to rule out any SetAsXML() (code below) leaks. Following is not showing any significant increase of memory in Task Manager (so technically, I don't know if it stopped memory leaks) (1). If I don't call Save() et all, then memory doesn't increase. But calling Save() is essential. (2). Declare a local string variable and assign a.asXML to it and pass it to Save(), then the memory increase stops. (3). When I apply casting string(a.asAXML) while calling Save(), the memory increase stops. Some Details: asXML - is a read and write property of type OleVariant. Its get method - GetAsXML(), uses lib2XML parser and RTTI (old style, TypInfo), builds up a XML structure of the object and return an OleVariant. function xxx.GetAsXML: OleVariant; var lDocument : xmlDocPtr; lRootNode: xmlNodePtr; lBuffer : xmlCharPtr; //PAnsiChar in LibXML2 lBufferSize : integer; lResult : string; lName : string; begin lDocument := xmlNewDoc(nil); try // ... Builds up the XMLDocuments here using RTTI xmlDocDumpMemory(lDocument, @lBuffer, @lBufferSize); try lResult := lBuffer; // Ansi converted in unicode string Result := lResult; // unicode string to olestr finally xmlFree(lBuffer); end; finally xmlFreeDoc(lDocument); end; end; procedure xxx.SetAsXML(const AInputVariant: OleVariant) var ... begin lOutputStream := TMemoryStream.Create; lOutputStream.Position := 0; if (VarType(AInputVariant) = varOleStr) then begin lStringStream := TStringStream.Create(String(AInputVariant)); // <--- Notice the casting try lStringStream.Position := 0; lOutputStream.CopyFrom(lStringStream, lStringStream.Size); finally lStringStream.Free; end; // Now process the lOutputStream using libXML2 and RTTI and assign back the properties. end end; Save() -- Takes input OleVariant, fills a local blank object with the data from OleVariant as setting local object's asXML := ABusinessObj Here is how Save() is defined in type library: interface Ixxx: Iyyy { [id(0x00000001)] HRESULT _stdcall Save([in] VARIANT ABusinessObj, ......) Can someone help to figure out what's going on? Why its leaking the memory, and not increasing when String() is applied, and what could be the clean solution?
- 14 replies
-
- rio
- olevariant
-
(and 1 more)
Tagged with: