0

I got a free: invalid pointer when running my project. But when I try to hunt the root cause using -fsanitize=address, the error disappears. When I remove the sanitizer, the error appears again. Can anyone give some hint about what was happening and how to pinpoint the problem in such a case? Thanks!

Update:

I understand it is hard to give suggestions without code. As the project is pretty big and I am still trying to build a minimal working example, I cannot provide any code right now. So I would like to change my question to a more general one:

According to my understanding of address sanitizer, it should track all allocation/access operations and report error if there is any invalid access. So if there is an error when not applying address sanitizer, the error should be still there after applying sanitizer. Is this understanding correct?

Community
  • 1
  • 1
Harper
  • 1,331
  • 11
  • 26
  • 5
    We call this a Heisenbug – M.M Jun 09 '20 at 21:51
  • 6
    Without code for context all we can really say is "Cool story." – tadman Jun 09 '20 at 21:52
  • 1
    The number of possible bugs in a C++ program is without limit. "Undefined behavior" means anything can happen. Including these results. This means any kind of undefined behavior is possible. Without a [mre], there's nothing that anyone can tell you. – Sam Varshavchik Jun 09 '20 at 21:55
  • 2
    You problem might be affected by undefined behavior like an uninitialized variable or a data race which can't be detected by Address Sanitizer. Enabling ASan might change the value an uninitialized variable has or change the timing slightly which can hide/expose certain types of problems. Try running Memory Sanitizer and Thread Sanitizer - maybe these tools can find issues? – Martin Konrad Jun 09 '20 at 23:22
  • @SamVarshavchik: That's true, but you have to admit that people are likely to search SO for something like the title of this question. – einpoklum Jun 10 '20 at 17:55

1 Answers1

1

Since you asked a vague and inspecific question, I'll provide a cliche and inspecific answer...

Try not to allocate and de-allocate directly, yourself

Read this SO question and its answers:

Why should C++ programmers minimize use of 'new'?

if you use containers (std::vector, std::array etc.), or smart pointers (std::unique_ptr, std::shared_ptr) - then allocation and de-allocation will be taken care of for you. You won't double-deallocate those resources. It is actually quite feasible to avoid new and delete altogether in many applications.

Dot your i's and cross your t's - with compiler warnings

Many issues are actually detected by the compiler, but are not technically invalid C++, so it lets them pass and only gives you a warning.

Make the effort to address all of your compiler warnings, and - compile with more warnings enabled. For example, with g++, use at least -W -Wall -Wextra, and there are more still.

An out-of-bounds write?

You may be writing out-of-bounds, or through a stale pointer, by mistake. That could possibly cause the pointer you're trying to free to get overwritten. To try to detect this, running your program with valgrind may be useful. See:

How does valgrind work?

an alternative is using another kind of sanitizer - a memory santizer (Msan). Read about the difference between those and valgrind to decide which would work for you:

Memory/Address Sanitizer vs Valgrind

einpoklum
  • 86,754
  • 39
  • 223
  • 453
  • Thanks for your answer! Yes I understand `free: invalid pointer` is mostly caused by writing out-of-bound and I am working towards that way to check the root cause. However one thing I don't understand, as I mentioned in the question update, is why this has something to do with the sanitizer? In my opinion, an out-of-bound write should always be out-of-bound no matter there is a sanitizer or not. – Harper Jun 09 '20 at 22:54
  • 1
    The instrumentation introduced into your code by the sanitizer may result in some changes in layout, or uninitialized value, or something else of that nature. So an out-of-bounds write might not have the same effect. – einpoklum Jun 10 '20 at 06:30