Delgine 3D Tools & Content DeleD Community Edition
Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Little memory leak in Plugin source code

 
Post new topic   Reply to topic    DeleD Community Edition Forum Index -> DeleD Plugins
View previous topic :: View next topic  
Author Message
Execute
Member


Joined: 30 Jan 2016
Posts: 5

PostPosted: Sat Feb 06, 2016 2:06 pm    Post subject: Little memory leak in Plugin source code Reply with quote

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 Smile

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
View user's profile Send private message
Jeroen
Site Admin


Joined: 07 Aug 2004
Posts: 5321
Location: The Netherlands

PostPosted: Sun Feb 07, 2016 10:11 am    Post subject: Reply with quote

Good find! Cool

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. Smile
_________________
Check out the gallery, our DeleD Wiki and Youtube videos!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Execute
Member


Joined: 30 Jan 2016
Posts: 5

PostPosted: Sun Feb 07, 2016 12:11 pm    Post subject: Reply with quote

Yes I know, you're working on a Web based version, very nice indeed Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DeleD Community Edition Forum Index -> DeleD Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum