memory error

Hello, I need help with an error in my program. The program runs but a variable initialized with 0 randomly changes value, although it is not set in currently running part of code.
It seems to me to be very difficult to find an error myself. Could anyone recommend me a tool or library under QNX which helps to detect such a problem?
Thanx, Tom

Tom,

Take a look at “Heap Analysis - Making Memory Error a thing of the past” in the Helpviewer Doc’s.

There is a special malloc debug library you can link against to help find this kind of error.

Tim

If the variable is on the stack the heap analysis won’t work ;-)

I think the debugger can set a breakpoint when a memory location gets written to. That would probably be your best bet.

I think your “string” or array before is not initialized and overwrites the var ^^

If the code isn’t too big post it.

Very true. Tom didn’t say whether is was a stack or heap variable that’s being corrupted though. I just assumed it was a heap variable.

I’ve tried this once or twice. It’s incredibly painfully slow to run. Plus I am not sure it can monitor stack variables since they are so short lived compared to heap based ones.

Stack corruption is usually much easier to track down than heap corruption (assuming it’s not a wild pointer just happening to point into stack memory) since it has to be something further down the calling tree corrupting you. So you can usually add a printf before/after each function call in the routine you have the variable declared in and see which is the offending call.

If you have a lot of calls, it sometimes is better to just compile in optimized mode (and generate a map file), hope for a sigsegv from the stack corruption, see the address, look in the map file and find the routine that’s corrupting the stack (usually someone writes off the end of an array).

Tim

Thanx for all answers.

Unfortunately because of more urgent work I didn’t have time to try your advices in practice till now.

It is heap variable, a field in an object. There is no string or array before it in that object, so I think there is something else wrong.
The code is in C++ and unfortunately too big to post it (several .cpp files).

I use C++ ‘new’ operator to allocate an object, so my question is: may I use malloc debug library to handle it or there is special C++ tool?

Tom

You best bet is to use the debugger to figure out when that variable gets overwritten. A debug malloc library may tell you something is corrupted but it`s very unlikely that it will tell you where and when it got corrupted.

Tom,

I believe the malloc debug library can help you with C++. Looking at the doc’s they don’t say you can’t use C++ and there is a section devoted to C++.

Is your program single or multi-threaded? If you are multi-threaded you are almost for sure going to need the debug malloc library as it’s unlikely the debugger can help you.

It might also be useful to describe what your program does or is doing when this problem occurs so we can give hints on where to look. Since it’s a heap variable getting corrupted it sounds like some part of the program is writing to an already free’d pointer.

Tim

Why?