View previous topic :: View next topic |
Author |
Message |
Execute Member
Joined: 30 Jan 2016 Posts: 5
|
Posted: Sat Feb 06, 2016 2:06 pm Post subject: Little memory leak in Plugin source code |
|
|
Hello,
I've take a look at the Plugin sources to make them Delphi Seattle compatible (AnsiString vs WideString)
But even under Delphi 6/7 the actual code is wrong
first tip, Delphi string is PAnsiChar compatible, there's always a #0 at the end of a string
procedure DeleDSendFileData(const aFileData: string);
var callBackData: TCallBackRecord;
begin
with callBackData do begin
RequestID := PR_SETDATA;
RequestXML := PChar(aFileData); //StrNew(PChar(aFileData));
ResponseXML := nil;
ResponseSize := Length(aFileData);
DeledCallBack(callBackData);
// StrDispose(RequestXML);
end;
end;
the second function can use the same fix and there's an error anyway on StrDispose...and there's no need to allocate the PChar twice
function DeleDRetrieveSceneData(const aRequestXML: AnsiString): AnsiString;
var callBackData: TCallBackRecord;
callBackResult: integer;
begin
if not Assigned(DeleDCallback) then exit;
// find out how much memory we need for the specific request
with callBackData do begin
RequestID := PR_GETMEM;
RequestXML := PAnsiChar(aRequestXML); //StrNew(PChar(aRequestXML));
ResponseXML := nil;
ResponseSize := 0;
callBackResult := DeledCallBack(callBackData);
// StrDispose(ResponseXML); <<<< wrong pointer !
end;
// retrieve the actual XML data
if callBackResult = PE_NOERROR then begin
with callBackData do begin
SetLength(Result, callBackData.ResponseSize);
FillChar(Result[1], Length(Result), 0);
RequestID := PR_GETDATA;
// RequestXML := StrNew(PChar(aRequestXML));
ResponseXML := PAnsiChar(Result);
callBackResult := DeledCallBack(callBackData);
// StrDispose(RequestXML);
end;
end;
// raise error if needed
if callBackResult <> PE_NOERROR then
raise Exception.Create(Format('DeleD callback returned error code %d', [callBackResult]));
end;
I use AnsiString and PAnsiChar for Delphi > 2009 of course.
BTW: a better option would be
with callBackData do begin
RequestID := PR_ALLOCATE_AND_GETDATA;
RequestXML := PAnsiChar(aRequestXML);
ResponseXML := nil;
ResponseSize := 0;
callBackResult := DeledCallBack(callBackData);
end;
with callBackData do begin
// copy response
SetString(Result, ResponseXML, ResponseSize);
RequestID := PR_FREEMEM; // release ResponseXML
callBackResult := DeledCallBack(callBackData);
end;
Regards
Paul TOTH |
|
Back to top |
|
|
Jeroen Site Admin
Joined: 07 Aug 2004 Posts: 5332 Location: The Netherlands
|
Posted: Sun Feb 07, 2016 10:11 am Post subject: |
|
|
Good find!
Btw, I'm not actively maintaining the DeleD & plugin sourcecode anymore (don't even have Delphi installed atm) so I'm not going to alter it in the code myself. But very nice job - and do keep at it, because I like learning things like this. _________________ Check out Figuro, our online 3D app! More powerful 3D tools for free. |
|
Back to top |
|
|
Execute Member
Joined: 30 Jan 2016 Posts: 5
|
Posted: Sun Feb 07, 2016 12:11 pm Post subject: |
|
|
Yes I know, you're working on a Web based version, very nice indeed |
|
Back to top |
|
|
|