Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-06-2012, 04:00 PM
sprocket sprocket is offline
Junior Member
 
Join Date: Feb 2011
Posts: 12
Default A 'SetLength()' Study...

Hi. First off, I'm working with Delphi 7. I thought I understood precisely what was involved when using SetLength() in a program until I started getting "Out Of Memory" errors when writing a program that extracts text from PDF's via Acrobats 'acrobat.tlb'. So I did a little study on the problem and was startled by what I found.

It's easily illustrated by running this program snippet with the debugger;

Quote:
program SetLength_study;

{$APPTYPE CONSOLE}

uses
SysUtils;

var Str1: String;
temp1,temp2,temp3,i,j,count: Integer;
TxtArr: array of String; // *** works properly when declared as 'Char' but not with 'String' ***

begin
{ TODO -oUser -cConsole Main : Insert code here }
j := 10000;
while j > 0 do
begin
temp1 := 1 + Random(7);
Str1 := '';
for i := 1 to temp1 do
Str1 := Str1 + Chr(Ord('a') + Random(25));
temp2 := High(TxtArr) + 1;
SetLength(TxtArr,temp1 + temp2);
temp3 := temp1 + temp2 - 1;
for i := temp2 to temp3 do
begin
TxtArr[i] := Str1[temp1];
Dec(temp1);
end;
dec(j);
end;
count := Length(TxtArr);
TxtArr := nil;
count := temp2;
end.
This simply generates letter-strings of between 1 & 7 characters, and increases the string-array 'TxtArr' by the appropriate length before adding the 'word' to its end. And it works perfectly, with either 'Length(TxtArr) or High(TxtArr) returning the expected final-size when it finishes looping - in this example, 39,604K.

What is positively alarming is the amount of 'real' memory that SetLength() is using - whereas it's creating an array of about 40K, it is actually using over 115meg by the time it finishes!!! And although like I said, Length(TxtArr) returns the 'expected' value, when I then clear the array with 'TxtArr := nil;' as above, all 115meg is immediately released. So SetLength() is definitely the culprit!

Is this a Delphi7 bug, or have I completely misunderstood how SetLength() is supposed to work???

Edit: Damn, I'm mortified to realise that I was mixing types 'String' and 'Char' up! I should of course have declared the array as type Char, not String - as noted above.

Live & (re)learn.

RE-edit: Sigh, I was too hard on my self first-time around - there is a bug in Delphi 7 string-handling.

Last edited by sprocket; 06-10-2012 at 07:01 AM.
Reply With Quote
  #2  
Old 06-10-2012, 07:01 AM
sprocket sprocket is offline
Junior Member
 
Join Date: Feb 2011
Posts: 12
Default

After a bit of a rethink I decided I was right after all - the above code should work properly whether the string-array is declared as type Char or String.

And quite by accident while trying to find an open-source way of importing text from PDF's - you must have that ludicrously expensive 'Adobe Acrobat' installed otherwise! - I came across this gem of a web-site;

http://synopse.info/forum/index.php

Not only do they offer completely free PDF-creation stuff (which I hope also includes the simple text-retrieval I'm looking to do) but I also came across a thread in the forum where they have enhanced the Delphi 7 Run-Time-Library;

http://synopse.info/forum/viewtopic.php?id=23

Rather than go through the somewhat elaborate patching of the sources, I just downloaded the already-compiled libraries, 'SysInit.dcu' and 'System.dcu' available here;

http://synopse.info/files/SynopseRTLdcu.zip

Once you replace the originals (in the 'Lib' folder) with these, the above code runs perfectly whether 'TxtArr' is declared as type 'Char' or 'String'.

btw, I originally thought that any such bugs would have been addressed in a 'Service Pack' of some kind, so was pleased to find that there was indeed one issued, 'Update 1' which is pretty easy to find on the web. Unfortunately, with it installed I still had the same 115-130meg memory-leak problem when I ran the above code with 'TxtArr' declared as type 'String'...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:20 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.