Click here to Skip to main content
14,356,475 members
   

Delphi

 
QuestionProblem with opc client ondatachange event Pin
dadio256-May-09 1:41
memberdadio256-May-09 1:41 
AnswerRe: Problem with opc client ondatachange event Pin
Moreno Airoldi29-May-09 10:07
professionalMoreno Airoldi29-May-09 10:07 
GeneralRe: Problem with opc client ondatachange event Pin
dadio2529-May-09 11:05
memberdadio2529-May-09 11:05 
QuestionADO and Memo field Pin
dave.kelly5-May-09 22:52
professionaldave.kelly5-May-09 22:52 
QuestionQuestion about adding items to TQUEUE Pin
Shannon Walker4-May-09 8:16
memberShannon Walker4-May-09 8:16 
Questionusing delphi dll in visual stduio Pin
tai-fun4-May-09 0:39
membertai-fun4-May-09 0:39 
AnswerRe: using delphi dll in visual stduio Pin
phannon865-May-09 6:39
professionalphannon865-May-09 6:39 
QuestionNeed help understanding memory leak [modified] Pin
Member 418743227-Apr-09 5:59
memberMember 418743227-Apr-09 5:59 
Hello!

I have some code which leaks memory (it's pretty much copied straight from Rudy's Delphi Corner). I have found the line that which causes the problem, but I don't understand why it's leaking which makes me concerned. I am reluctant to just fix it without knowing what's actually wrong.

The purpose of the code is to create a separate copy of a TVarRec variable. Here goes:
function TfrmMain.CopyVarRec(const Item: TVarRec): TVarRec;
begin
  // Copy entire TVarRec first.
  Result := Item;

  // Now handle special cases.
  case Item.VType of
    vtExtended:
      begin
        New(Result.VExtended);
        Result.VExtended^ := Item.VExtended^;
      end;
    vtPChar:
      begin
        Result.VPChar := StrNew(Item.VPChar);
      end;
    // A little trickier: casting to string will ensure
    // reference counting is done properly.
    vtAnsiString:
      begin
        // Nil out first, so no attempt to decrement
        // reference count.
        Result.VAnsiString := nil;
        string(Result.VAnsiString) := string(Item.VAnsiString);
      end;
  end;
end;


The following function is then used to free the copied TVarRec:
// TVarRecs created by CopyVarRec must be finalized with this function.
// You should not use it on other TVarRecs.
procedure TfrmMain.FinalizeVarRec(var Item: TVarRec);
begin
  case Item.VType of
    vtExtended: Dispose(Item.VExtended);
    vtString: Dispose(Item.VString);
    // vtAnsiString uses reference counting.
  end;
  Item.VInteger := 0;
end;


The above code leaks memory when the TVarRec contains an AnsiString, that is in the vtAnsiString case of the CopyVarRec function. The weird thing is, the memory leak goes away if I remove the line
Result.VAnsiString := nil;
before assigning the Item's string to teh string of the the Result. If I remove it, the memory leak goes away and (it appears) the program works fine, but I really don't feel comfortable doing so without understanding why that line causes the problem to begin with.

Any ideas?

Regards,
Daniel

modified on Monday, April 27, 2009 11:37 AM

AnswerRe: Need help understanding memory leak Pin
Member 418743227-Apr-09 7:34
memberMember 418743227-Apr-09 7:34 
QuestionLooking for Interbase 6.0 (download, install, client) Pin
Eddy Vluggen22-Apr-09 10:36
mveEddy Vluggen22-Apr-09 10:36 
AnswerRe: Looking for Interbase 6.0 (download, install, client) Pin
Henry Minute22-Apr-09 11:41
memberHenry Minute22-Apr-09 11:41 
GeneralRe: Looking for Interbase 6.0 (download, install, client) Pin
Eddy Vluggen22-Apr-09 12:09
mveEddy Vluggen22-Apr-09 12:09 
NewsRe: Looking for Interbase 6.0 (download, install, client) Pin
akirilov24-Apr-09 3:38
memberakirilov24-Apr-09 3:38 
GeneralRe: Looking for Interbase 6.0 (download, install, client) Pin
Eddy Vluggen24-Apr-09 3:39
mveEddy Vluggen24-Apr-09 3:39 
GeneralRe: Looking for Interbase 6.0 (download, install, client) Pin
phannon8624-Apr-09 4:53
professionalphannon8624-Apr-09 4:53 
GeneralRe: Looking for Interbase 6.0 (download, install, client) Pin
ros446-Sep-11 0:04
memberros446-Sep-11 0:04 
GeneralRe: Looking for Interbase 6.0 (download, install, client) Pin
MorpheusQ20-Oct-11 5:50
memberMorpheusQ20-Oct-11 5:50 
QuestionI need help with compiling a Delphi 7 project... Pin
SimpleData12-Apr-09 5:41
memberSimpleData12-Apr-09 5:41 
AnswerRe: I need help with compiling a Delphi 7 project... Pin
Amarnath S14-Apr-09 4:01
professionalAmarnath S14-Apr-09 4:01 
AnswerRe: I need help with compiling a Delphi 7 project... Pin
Richard A. Abbott14-Apr-09 4:57
professionalRichard A. Abbott14-Apr-09 4:57 
GeneralRe: I need help with compiling a Delphi 7 project... Pin
SimpleData14-Apr-09 6:00
memberSimpleData14-Apr-09 6:00 
GeneralRe: I need help with compiling a Delphi 7 project... Pin
Richard A. Abbott14-Apr-09 7:04
professionalRichard A. Abbott14-Apr-09 7:04 
GeneralRe: I need help with compiling a Delphi 7 project... Pin
SimpleData14-Apr-09 7:16
memberSimpleData14-Apr-09 7:16 
AnswerRe: I need help with compiling a Delphi 7 project... Pin
chambers120@suddenlink1-May-09 3:19
memberchambers120@suddenlink1-May-09 3:19 
GeneralRe: I need help with compiling a Delphi 7 project... Pin
SimpleData1-May-09 4:14
memberSimpleData1-May-09 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.