Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to handle access violation with GetTempPath function

113 views
Skip to first unread message

Stephane Veillette

unread,
Jun 12, 2000, 3:00:00 AM6/12/00
to
Hi,
I'm using GetTempPath function but I always got "access violation" at the
end of the function (not during execution). If I remove the GetTempPath
function from the program, everything is ok.

Is there an other way to retrieve windows temporary path from the registry?

_________________________________
Here's the code :

function ...
var
Temp : PChar;
begin
try
GetTempPath(max_path,Temp);
except
end;
end;

Regards,
Stephane

Stephane Veillette

unread,
Jun 12, 2000, 3:00:00 AM6/12/00
to
Thank you Shannon, it works!!!!

ś:-)
Stephane

Shannon Broskie <shannon...@tagfolio.com> wrote in message
news:39453eef@dnews...
> Here's what I use...
>
> procedure GetTempWorkPath;
> (* Procedure: GetTempWorkPath
> *)
> (* Description: Place the Windows temporary path into the TempPath global
> *)
> (* variable.
> *)
> var
> PPath : array[0..255] of char;
> begin
> GetTempPath (SizeOf (PPath), PPath);
> TempPath := Trim(PPath);
> end;
>
> "Stephane Veillette" <veill...@biotonix.com> wrote in message
> news:8i3bi2$ct...@bornews.borland.com...

Shannon Broskie

unread,
Jun 12, 2000, 3:00:00 AM6/12/00
to

Steve Schafer (TeamB)

unread,
Jun 12, 2000, 3:00:00 AM6/12/00
to
On Mon, 12 Jun 2000 14:55:12 -0400, "Stephane Veillette"
<veill...@biotonix.com> wrote:

>I'm using GetTempPath function but I always got "access violation" at the
>end of the function (not during execution).

You never allocate memory to hold the result of GetTempPath. a PChar
variable is merely a pointer; until you allocate memory for it, it
points to a random memory address, and that's what causes the access
violation.

Using a PChar, you'd need to do something like this:

var
Temp: PChar;
begin
Temp := StrAlloc(MAX_PATH);
try
GetTempPath(MAX_PATH, Temp);
// use the result here
finally
StrDispose(Temp) end end;

>begin
> try
> GetTempPath(max_path,Temp);
> except
> end;
>end;

I want you to go to the blackboard right now and write the following
sentence 1000 times:

I will NEVER again write an empty EXCEPT clause.
I will NEVER again write an empty EXCEPT clause.
I will NEVER again write an empty EXCEPT clause.
...

By writing an empty EXCEPT clause, you are saying "I don't care if my
program contains a nasty bug that could corrupt data or cause the
computer to crash; I don't want to know about it."

-Steve


Davie

unread,
Jun 14, 2000, 3:00:00 AM6/14/00
to
The problem with your code is that you variable TEMP is a pointer to a bunch of
chars. It seems that you have not pointed this pointer to anywhere in memory
that is valid. So, it's just pointing to basically who knows where. Maybe
sometimes it's pointing within a valid area, and maybe sometimes, it's pointing
to an invalid spot. And just because it points to a spot that doesn't give you
an AV error doesn't mean that it's ok. Baically you need to point it somewhere
valid and somewhere that YOU setup. So, for example, you could get memory and
then assign the pointer to the block of memory you got.

GetMem(Temp,Max_path+1);

That would assign a bunch of bytes to a pointer TEMP and then your GetTempPath
would work, then before exiting your routine you shouold free up the memory you
just allocated otherwise you end up with memory leaks :) So then you could do
this

FreeMem(Temp, Max_path+1);

Davie

Stephane Veillette wrote:

> Hi,


> I'm using GetTempPath function but I always got "access violation" at the

> end of the function (not during execution). If I remove the GetTempPath
> function from the program, everything is ok.
>
> Is there an other way to retrieve windows temporary path from the registry?
>
> _________________________________
> Here's the code :
>
> function ...
> var
> Temp : PChar;

> begin
> try
> GetTempPath(max_path,Temp);
> except
> end;
> end;
>

> Regards,
> Stephane


0 new messages