| Author |
Message |
|
|
Post subject: memory error
Posted: Feb 13, 2008 - 03:19 PM
|
|
New Member
Joined: Feb 13, 2008
Posts: 2
|
|
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 |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 13, 2008 - 03:49 PM
|
|
Senior Member
Joined: Mar 10, 2004
Posts: 512
|
|
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 |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 13, 2008 - 05:05 PM
|
|
QNX Master
Joined: Sep 01, 2002
Posts: 2667
|
|
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. |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 13, 2008 - 05:07 PM
|
|
Senior Member
Joined: Jul 22, 2004
Posts: 315
|
|
| I think your "string" or array before is not initialized and overwrites the var ^^ |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 13, 2008 - 05:08 PM
|
|
QNX Master
Joined: Sep 01, 2002
Posts: 2667
|
|
| If the code isn't too big post it. |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 13, 2008 - 06:09 PM
|
|
Senior Member
Joined: Mar 10, 2004
Posts: 512
|
|
|
mario wrote:
If the variable is on the stack the heap analysis won't work
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.
mario wrote:
I think the debugger can set a breakpoint when a memory location gets written to. That would probably be your best bet.
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 |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 18, 2008 - 09:14 AM
|
|
New Member
Joined: Feb 13, 2008
Posts: 2
|
|
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 |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 18, 2008 - 12:59 PM
|
|
QNX Master
Joined: Sep 01, 2002
Posts: 2667
|
|
| 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. |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 18, 2008 - 03:26 PM
|
|
Senior Member
Joined: Mar 10, 2004
Posts: 512
|
|
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 |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 20, 2008 - 03:12 AM
|
|
QNX Master
Joined: Sep 01, 2002
Posts: 2667
|
|
|
Tim wrote:
Tom,
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.
Tim
Why? |
|
|
| |
|
|
|
 |
|
|