How do we find out stepping over somebody else' memory

N

NewToCPP

There are several occations where we write onto someone else' memory
region. Is there any debugging mechanism to find out which part of the
code is causing this problem?
 
P

peter steiner

NewToCPP said:
There are several occations where we write onto someone else' memory
region. Is there any debugging mechanism to find out which part of the
code is causing this problem?

i am not sure what you mean by "someone else'" memory.

if you mean writing foreign process memory that should be easy to
detect in any debugger as the operating system protects such access
violations. any debugger should break at the offending statement.

on the other hand, if you thought about writing into your own memory
off bounds, eg. by overflowing an array or writing into freed memory,
you will have to use tools that enable memory bounds checking. with
such a tool any debugger should provide the same method as above by
breaking at offending statements.

examples are efence for linux
(http://perens.com/FreeSoftware/ElectricFence/) or the page heap
verification mechanism in windows (gflags.exe).

-- peter
 
D

deane_gavin

NewToCPP said:
There are several occations where we write onto someone else' memory
region. Is there any debugging mechanism to find out which part of the
code is causing this problem?

You've started three threads today all about tracking down dynamic
memory bugs. In one of those other threads, someone suggested avoiding
all direct management of memory by using classes that do it for you.
std::vector or another container for collections of objects. Smart
pointers (std::auto_ptr where appropriate, or maybe something from
boost) for single objects. std::string instead of arrays of char. RAII
techniques. And, importantly, only dynamically allocating memory (even
when using classes designed for the purpose) when automatic storage
duration isn't sufficient.

This really is the single biggest step you can take towards avoiding
these bugs. It will be an easy step to take from this moment on with
any new code you write, a harder step to take when maintaining existing
code.

I know I haven't answered your particular question here (I'm not sure
what you mean by "someone else's memory" - another process, another
object in your program, or what?). But I wanted to reiterate the point
that, when you must use dynamic memory, use classes designed to take
the risks away.

Gavin Deane
 
N

NewToCPP

Here I was referring to memory created by another object in the same
program. For example some memory created by different task (or thread)
in the same program.
 
R

red floyd

NewToCPP said:
Here I was referring to memory created by another object in the same
program. For example some memory created by different task (or thread)
in the same program.

Then definitely follow my advice from the other thread. Use standard
containers. Pick an appropriate smart pointer and use it. Use
std::string. Create objects rather than pointers to objects whenever
possible.

If you're worried about threading, then the stock answer is that the C++
Standard doesn't address threading.

That said, check the synchronization primitives (mutexes, events,
critical sections, condition variables) that your platform provides, and
use them to protect your data structures in when necessary.

For further details on synchronization primitives, ask in a newsgroup
dedicated to your platform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top