Groups > Borland > Delphi upgrade > Re: D5 to D2007 FreeMem(pDriver, MAS_PATH)




D5 to D2007 FreeMem(pDriver, MAS_PATH)

D5 to D2007 FreeMem(pDriver, MAS_PATH)
Sat, 15 Mar 2008 00:37:53 -040
        GetMem(pDevice, cchDeviceName);
        GetMem(pDriver, MAX_PATH);
        GetMem(pPort, MAX_PATH);
        GetProfileString('Devices', pchar(scboPrinter), '', pDriver, 
MAX_PATH);
        pDriver[pos(',', pDriver) - 1] := #0;
        GetProfileString('Devices', pchar(scboPrinter), '', pPort, 
MAX_PATH);
        lStrCpy(pPort,  @pPort[lStrLen(pPort)+2]);
        report.SelectPrinter(StrPas(pdriver),scboPrinter,StrPas(pPort));
        report.SelectPrinter(StrPas(pdriver),scboPrinter,StrPas(pPort));
        FreeMem(pDevice, cchDeviceName);
        FreeMem(pDriver, MAX_PATH); 
<<<<<<<<<<<<<<<<<<Error
Line
        FreeMem(pPort, MAX_PATH);

Any ideas?
-- 
Tony Nasca
Dove Net Technologies, LLC
9126 Travener Circle, Suite 100
Frederick, MD 21704
301-874-9777 x 100 Fax 301-874-9767
www.dovenet.com
nasca@dovenet.com 

Post Reply
Re: D5 to D2007 FreeMem(pDriver, MAS_PATH)
15 Mar 2008 04:03:06 -0700
Anthony Nasca wrote:

>        GetMem(pDevice, cchDeviceName);
>        GetMem(pDriver, MAX_PATH);
>        GetMem(pPort, MAX_PATH);
>       GetProfileString('Devices', pchar(scboPrinter), '', pDriver,
MAX_PATH);


>        pDriver[pos(',', pDriver) - 1] := #0;

Consider what happens here if pDriver does not contain a ',' .....

>       GetProfileString('Devices', pchar(scboPrinter), '', pPort,
MAX_PATH);
>        lStrCpy(pPort,  @pPort[lStrLen(pPort)+2]);

What is that supposed to accomplish, other than a random memory
overwrite?

If the goal is to split the comma-separated list of strings returned by
GetProfileString do it this way:

       GetMem(pDriver, MAX_PATH);
       GetProfileString('Devices', pchar(scboPrinter), '', pDriver,
MAX_PATH);
       pPort := StrScan(pDriver, ',');
       if pPort = nil then
         ...raise an exception, there is no comma in this string
      pPort^ := #0;
      Inc(pPort);

DO NOT CALL FreeMem ON pPort!!! It just refers to a part of the pDriver
string.



-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://www.tamaracka.com/search.htm
Post Reply
about | contact