How to properly debug and find the causes of failures?

I do not know what to do next ... its hopeless. I get tired of guessing what causes the accident. I recently noticed that some opengl calls accidentally run programs on some gfx cards. therefore, I am becoming really paranoid, which can lead to failures now. The bad thing about this crash is that it crashes only after a long time using the program, so I can only guess what the problem is.

I can’t remember what changes I made to the program, which can cause crashes, it took so long. But, fortunately, the previous version does not crash, so I can just copy some code and spend 10 hours to see at what point it starts to crash ... I don’t think I want to do it yet.

The program crashes after I process these files about 5 times in a row, each time it uses about 200 megabytes of memory. It is interrupted randomly during and after the reading process.

I create a "safe" function free (), it checks the pointer if it is not NULL, and then frees memory, and then sets the pointer to NULL. Shouldn't this be done?

I watched the memory of the task manager, and before it crashed, he started eating 2 times more memory than usual. Also, loading the program became exponentially slower every time I downloaded files; the first few loads did not look much slower apart, but then he quickly began to double download speeds. What should this tell me about the accident?

Also, do I need to manually release C ++ vectors using clear ()? Or are they freed after use automatically, for example, if I select a vector inside a function, will it be freed every time the function ends? I do not save pointers in a vector.

-

Soon: I want to learn how to catch damned mistakes as quickly as possible, how to do this? Using Visual Studio 2008.

+6
c ++ visual-studio-2008 windows-xp opengl
source share
7 answers

A “random” accident that occurs sometime after a complex operation is almost certainly the result of a heap. Heap corruption errors are a bitch, as they usually manifest very far from the place that actually caused the error. My suggestion, since you are on Windows, is to use Application Verifier , which can be downloaded freely from MS.

Launch AV, configure it to view your program, and turn on all memory related options. Then run the program under the debugger. (These two things will make your program run very slowly.) Additional checks that AV does, we hope your program crashes in a different place than you have seen so far, but it will be the place that is the real cause of the error.

+3
source share

Most likely you are getting memory corruption. You want a memory debugger to track this; You can find recommendations here .

I create a "safe" function free (), it checks the pointer if it is not NULL, and then frees memory, and then sets the pointer to NULL.

A NULL check is not required because the standard compatible (which VS2008) will perform this check for you. You can still free up the junk mail pointer, which can cause problems.

Regarding setting the pointer to NULL, this may help, but it is not a panacea. First, you cannot set the correct pointer to NULL. For example, if you do this:

void myfree(void *ptr) { free(ptr); ptr = NULL; } 

all you have done is set the local myfree parameter to NULL. A copy of the pointer is still untouched.

+1
source share

As before, this is most likely a memory leak.

Visual Studio has built-in tools for this.

Basically you take two snapshots of memory and compare them after a while

+1
source share

First question, why are you using free () in C ++? Usually you should use new / delete to manage memory, or better, new in combination with smart pointers.

You do not need to manually clean () the vectors. Keep in mind that if you store pointers to objects in a vector (which is not necessarily good practice, there are better containers for this), you will have to destroy them separately before the vector is destroyed. As you said, you do not store pointers in a vector, the vector will be destroyed when it goes out of scope - that is, the containing object is destroyed or the program leaves the area inside the function.

Random crashes are usually / often memory errors, I would recommend that you equip yourself with a good memory debugger. On Windows, this usually means Rational Purify or Boundschecker. Of course, this helps to find out what caused the crash in the first place - not to cope with the state from memory? Zero pointer? Cosmic rays?

Given the response to the comments in your question, here is what I will do:

  • Modify the VS2008 exception catch so that it violates an access violation.
  • Run the program under the debugger, in debug mode - do not try to debug the release executable
  • Try to reproduce the behavior that you see. It will take longer than in release mode, but you should get it in the end. You will probably also get some false positives.
  • If you have a good idea that the (null) pointer gets dereferenced, assert () the pointer and runs the program again in the debugger.
+1
source share

“I can’t remember what changes I made to the program that could cause crashes, it took so long. But fortunately, the previous version does not crash, so I could just copy the code and the waste 10 hours to see which indicates that it is starting to crash .. "

This is why Test-Driven Development (TDD). Read it and see how it can help you avoid these scenarios.

+1
source share

Pay attention to the point of Noah Roberts and JJ Bangs - if you are not familiar with the debugger, it looks like you should get to know each other.

I also suggest a tool similar to valgrind - see this SO question .

Also, do I need to manually release C ++ vectors using clear ()?

Not.

0
source share

You really need to learn how to use a debugger. Click the play button at the top or use the functions of the remote debugger, if necessary. If you cannot track it this way, then drwtsn32 is also an option.

0
source share

All Articles