 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Feb 04, 2004 2:14 am Post subject: Access violation when allocating memory in windows |
|
|
Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in while
the function causes access violation message in debugger. If program runs
outside delphi the application closes with no error or warning. When this
problem happens it is repeatable and even rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it. The program simply
vanishes! In debug mode where I can see the access violation message the
CPU window shows instruction:
0040D24D push ebp
I tried on different machines to make sure there is no hardware problem. The
problem shows up on other machines as well. I just pass a pointer to a
record of size 512byte to New function.
Any idea to resolve this will be greatly appreciated.
Thanks,
RD.
|
|
| Back to top |
|
 |
Dragon Lord Guest
|
Posted: Wed Feb 04, 2004 2:29 am Post subject: Re: Access violation when allocating memory in windows |
|
|
show the code
Jeremy
<insc199 (AT) sbcglobal (DOT) net> wrote
| Quote: | Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in
while
the function causes access violation message in debugger. If program runs
outside delphi the application closes with no error or warning. When this
problem happens it is repeatable and even rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it. The program simply
vanishes! In debug mode where I can see the access violation message the
CPU window shows instruction:
0040D24D push ebp
I tried on different machines to make sure there is no hardware problem.
The
problem shows up on other machines as well. I just pass a pointer to a
record of size 512byte to New function.
Any idea to resolve this will be greatly appreciated.
Thanks,
RD.
|
|
|
| Back to top |
|
 |
Michael Brown Guest
|
Posted: Wed Feb 04, 2004 7:44 am Post subject: Re: Access violation when allocating memory in windows |
|
|
[email]insc199 (AT) sbcglobal (DOT) net[/email] wrote:
| Quote: | Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in
while the function causes access violation message in debugger. If
program runs outside delphi the application closes with no error or
warning.
|
This is exactly what happens if you overflow an allocated block (eg:
allocate a 100 byte buffer, then write 101 bytes into it), also known as
heap corruption. They're a pain to track down as the AV usually occurs in a
completely different area to where the problem is.
| Quote: | When this problem happens it is repeatable and even
rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it.
|
It probably won't be catchable because the heap will be in an invalid state,
and when it tries to allocate memory for the string it will fail again,
causing another exception, and so on until it dies.
| Quote: | The program
simply vanishes! In debug mode where I can see the access violation
message the CPU window shows instruction:
0040D24D push ebp
|
The Delphi debugger is not good at catching this sort of thing. The
exception is probably happening somewhere else altogether. You'll need to
download a real assembly debugger (I recommend OllyDBG) to see where the
exception is actually being thrown. Of course, you won't have the source
code view, but it can help narrow things down a bit with a bit of skill. The
best way to track down this sort of thing is to figure out how to repeat the
AV, and then look through each procedure what's invlolved in that process
and make sure you're not overflowing anything. Good luck :)
[...]
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz - My inbox is always open
|
|
| Back to top |
|
 |
Ryan Guest
|
Posted: Wed Feb 04, 2004 8:35 am Post subject: Re: Access violation when allocating memory in windows |
|
|
Are you freeing the memory you allocated once you finished with it ?
If not, try using FreeMem(yourBuffer) at the end of your procedure.
"Dragon Lord" <invalid (AT) joke (DOT) com> wrote
| Quote: | show the code
Jeremy
[email]insc199 (AT) sbcglobal (DOT) net[/email]> wrote in message
news:8AYTb.19878$HA.16510 (AT) newssvr27 (DOT) news.prodigy.com...
Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in
while
the function causes access violation message in debugger. If program runs
outside delphi the application closes with no error or warning. When this
problem happens it is repeatable and even rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it. The program simply
vanishes! In debug mode where I can see the access violation message the
CPU window shows instruction:
0040D24D push ebp
I tried on different machines to make sure there is no hardware problem.
The
problem shows up on other machines as well. I just pass a pointer to a
record of size 512byte to New function.
Any idea to resolve this will be greatly appreciated.
Thanks,
RD.
|
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Wed Feb 04, 2004 5:34 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
"Michael Brown" <see (AT) signature (DOT) below> wrote
| Quote: | code view, but it can help narrow things down a bit with a bit of skill.
The
best way to track down this sort of thing is to figure out how to repeat
the
AV, and then look through each procedure what's invlolved in that process
and make sure you're not overflowing anything. Good luck
|
Ideally one should make use of Delphi's strong typing and rich type
definition features so that these sorts of errors are not even possible. IOW
try, when ever possible, to avoid use of direct memory allocation (AllocMem,
etc.).
|
|
| Back to top |
|
 |
Dragon Lord Guest
|
Posted: Wed Feb 04, 2004 6:50 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
Yes, in fact, i've never really found a need to call the raw memory
allocators in delphi or the windows api. The furthest I get is to, New and
Dispose of Pointer typed records, and even then its rare.
Jeremy
----- Original Message -----
From: "Bruce Roberts" <ber (AT) bounceitattcanada (DOT) xnet>
Newsgroups: comp.lang.pascal.delphi.misc
Sent: Wednesday, February 04, 2004 12:34 PM
Subject: Re: Access violation when allocating memory in windows
| Quote: |
"Michael Brown" <see (AT) signature (DOT) below> wrote in message
news:H82Ub.33676$9k7.716334 (AT) news (DOT) xtra.co.nz...
code view, but it can help narrow things down a bit with a bit of skill.
The
best way to track down this sort of thing is to figure out how to repeat
the
AV, and then look through each procedure what's invlolved in that
process
and make sure you're not overflowing anything. Good luck :)
Ideally one should make use of Delphi's strong typing and rich type
definition features so that these sorts of errors are not even possible.
IOW
try, when ever possible, to avoid use of direct memory allocation
(AllocMem,
etc.).
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 04, 2004 7:13 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
Thanks for the pointer. You are correct. The problem was a few lines before.
I was over writing the stack by four bytes!
Appreciate the help.
"Michael Brown" <see (AT) signature (DOT) below> wrote
| Quote: | insc199 (AT) sbcglobal (DOT) net wrote:
Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in
while the function causes access violation message in debugger. If
program runs outside delphi the application closes with no error or
warning.
This is exactly what happens if you overflow an allocated block (eg:
allocate a 100 byte buffer, then write 101 bytes into it), also known as
heap corruption. They're a pain to track down as the AV usually occurs in
a
completely different area to where the problem is.
When this problem happens it is repeatable and even
rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it.
It probably won't be catchable because the heap will be in an invalid
state,
and when it tries to allocate memory for the string it will fail again,
causing another exception, and so on until it dies.
The program
simply vanishes! In debug mode where I can see the access violation
message the CPU window shows instruction:
0040D24D push ebp
The Delphi debugger is not good at catching this sort of thing. The
exception is probably happening somewhere else altogether. You'll need to
download a real assembly debugger (I recommend OllyDBG) to see where the
exception is actually being thrown. Of course, you won't have the source
code view, but it can help narrow things down a bit with a bit of skill.
The
best way to track down this sort of thing is to figure out how to repeat
the
AV, and then look through each procedure what's invlolved in that process
and make sure you're not overflowing anything. Good luck :)
[...]
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz - My inbox is always open
|
|
|
| Back to top |
|
 |
Dragon Lord Guest
|
Posted: Wed Feb 04, 2004 7:44 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
"Michael Brown" <see (AT) signature (DOT) below> wrote
| Quote: | insc199 (AT) sbcglobal (DOT) net wrote:
Hi,
I am trying to allocate memory using New, Allocmem or GetMem. Once in
while the function causes access violation message in debugger. If
program runs outside delphi the application closes with no error or
warning.
This is exactly what happens if you overflow an allocated block (eg:
allocate a 100 byte buffer, then write 101 bytes into it), also known as
heap corruption. They're a pain to track down as the AV usually occurs in
a
completely different area to where the problem is.
When this problem happens it is repeatable and even
rebooting the system won't help.
Unfotunatelly, the try...except block won't catch it.
It probably won't be catchable because the heap will be in an invalid
state,
and when it tries to allocate memory for the string it will fail again,
causing another exception, and so on until it dies.
The program
simply vanishes! In debug mode where I can see the access violation
message the CPU window shows instruction:
0040D24D push ebp
The Delphi debugger is not good at catching this sort of thing. The
exception is probably happening somewhere else altogether. You'll need to
download a real assembly debugger (I recommend OllyDBG) to see where the
exception is actually being thrown. Of course, you won't have the source
code view, but it can help narrow things down a bit with a bit of skill.
The
best way to track down this sort of thing is to figure out how to repeat
the
AV, and then look through each procedure what's invlolved in that process
and make sure you're not overflowing anything. Good luck :)
[...]
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz - My inbox is always open
|
I use my delphi source code in Ollydbg all the time.
1) Set project options
Under Compiler:
Unchecking: Optimization, sometimes helps but can be checked
Check All Debugging options, exception Definitions only
Check "Use Debug DCUs" if you want debugging symbols for the system
units, if you want source for some of the units, copy over the unit you want
source to, into your project directory
Under Linker: This is the most important.
Check include TD32 debug Info.
2) Compile your application, and make sure the exe is in your projects
directory
3) start up ollydbg, then start your application through it, to view your
source code in olly, just goto view->source or view->source files. You can
set breakpoints in your source code, or follow the source when you step
through the assembly. I for one, like the ollydbg debugger interface, more
than the one that comes with delphi. If you want it to follow your source
code to different source files, i recommend setting in the options under the
CPU tab. "Synchronize source with CPU". Also, just like the delphi
debugger, it breakpoints on exceptions, which there are some defaults in the
delphi debugger to ignore, you may have to include those in the "Debug" Tab.
Jeremy
|
|
| Back to top |
|
 |
Michael Brown Guest
|
Posted: Wed Feb 04, 2004 11:35 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
Bruce Roberts wrote:
| Quote: | "Michael Brown" <see (AT) signature (DOT) below> wrote in message
news:H82Ub.33676$9k7.716334 (AT) news (DOT) xtra.co.nz...
code view, but it can help narrow things down a bit with a bit of
skill. The best way to track down this sort of thing is to figure
out how to repeat the AV, and then look through each procedure
what's invlolved in that process and make sure you're not
overflowing anything. Good luck :)
Ideally one should make use of Delphi's strong typing and rich type
definition features so that these sorts of errors are not even
possible. IOW try, when ever possible, to avoid use of direct memory
allocation (AllocMem, etc.).
|
It's not so much direct memory allocation that the problem is, but more for
arrays. Any time when you have a dynamic array you potentially face this
problem. For example, if you do
SetLength(AArray, NumEntries);
// Some more code in here, or possibly a completely different function
for ALoop := 0 to NumEntries do
AArray[ALoop] := 1;
I do quite a bit of porting between C and Pascal, and more often that I
should I forget to subtract 1 off the high bound, causing problems like
this. Fortunately, I know I do this, so finding such problems is not too
much trouble :)
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz - My inbox is always open
|
|
| Back to top |
|
 |
Rob Kennedy Guest
|
Posted: Wed Feb 04, 2004 11:55 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
Michael Brown wrote:
| Quote: | It's not so much direct memory allocation that the problem is, but more for
arrays. Any time when you have a dynamic array you potentially face this
problem.
|
That's what the range-checking compiler option is for.
--
Rob
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Fri Feb 06, 2004 5:41 pm Post subject: Re: Access violation when allocating memory in windows |
|
|
"Rob Kennedy" <me (AT) privacy (DOT) net> wrote
| Quote: | That's what the range-checking compiler option is for.
|
Which I always keep on, even in release code. Another good idea is to use
the Low and High functions whenever possible for array bounds.
|
|
| Back to top |
|
 |
J French Guest
|
Posted: Sat Feb 07, 2004 8:25 am Post subject: Re: Access violation when allocating memory in windows |
|
|
On Fri, 6 Feb 2004 12:41:25 -0500, "Bruce Roberts"
<ber (AT) bounceitattcanada (DOT) xnet> wrote:
| Quote: |
"Rob Kennedy" <me (AT) privacy (DOT) net> wrote in message
news:bvs0ps$102i7e$1 (AT) ID-220940 (DOT) news.uni-berlin.de...
That's what the range-checking compiler option is for.
Which I always keep on, even in release code. Another good idea is to use
the Low and High functions whenever possible for array bounds.
|
Seconded - in both cases
|
|
| Back to top |
|
 |
|
|
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
|
|