How to fix heap corruption in c/c++? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
This is a further question for my previous one here.
Is there a general solution for this?

Fix all dangling pointers
Fix all buffer overflows
Use pointers only where they are really needed

Reading your original post I'm not 100% you are facing a heap-corruption and you really hope you don't because if you would this is one of the most tricky errors to track down and AFAIK there are no solutions that always work.
However, if you are positive its a heap-corruption and you are on the windows platform you could try out the tool gflags. This advanced debugging tools allow you to install a debug heap manager in order to make it possible to find certain kinds of heap corruptions. One of the neat options it has is to make each allocation in it's own page or to write protect the heap datastructures. This makes it easier to pinpoint the exact location of the heap-corruption. It sucks lots of memory and CPU though.
Another tip if you are using Visual Studio is that if you manage to find that something is corrupting the data after a certain point of time and you wish to find the code that corrupts the data you can use Data Breakpoint to break whenever someone changes the data in question.
I wish you good luck.

Adding to swegi's answer I would check all your C++ constructors and all C initializations. Transform all dynamic C++ constructors (those where you put the init code in the function body) into static ones (where you initialize with the special constructor syntax). And be sure that you init all pointers and pointer members to 0. For C, I would initialize all variables.
Then, on unix I would use valgrind, usually this is quite good in finding all access violations and if you compile with all debugging options on it is able to trace it to the source line. There should be something similar on windows, too.

Related

Why the OS allocates more memory than deemed necessary for a small sized executable? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Ok so I wrote this minimal C code and complied it to an executable in release mode
1.void main()
2.{ for(;;); } //this is here to make the app hang.
The executable file size itself is 6kb, I didn't include any headers. Even if the entire exe file gets copied to the ram, apparently it shouldn't occupy more than 7 kb, nevertheless the OS allocates 320 kb, why is that? I'm using windows.
It seems like you're confused and mixing a wide variety of concepts. Let me try to explain:
That program is very clearly an infinite loop which explains why it doesn't end (or what you call "hang").
The compiler/linker still need to write a valid executable for your OS, and this involves a bunch of headers and stuff, which could be easily consuming 6kb.
320kb in a mainstream OS at this point in time seems like almost nothing and it can mainly be OS overhead. It is hard to say more without knowing what OS.
I strongly encourage you to disassemble your code. Another option is to play with your compiler options to optimize for executable size. I think the bottom line is that you're expecting that since your program doesn't do anything useful its size should be zero, and this is an unreasonable expectation.

Dynamic allocation as a parameter, so to speak? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Assume we have a vector of Cursor object pointers. A cursor object is constructed with a single int parameter. So will this syntax work?
vector<cursor*> cursors;
cursors.push_back(new cursor(4));
Or do I have to do it as:
cursor* tempCursor = new cursor(4);
cursors.push_back(tempCursor);
Despite the fact that you may not have tried this here's an explanation of what's going on:
when you create a new cursor object it returns a new cursor object. When you use a push_back function it pushes an object back on a vector. So, when you create a new object inside the push_back it evaluates that function which returns a new cursor which then gets pushed back.
Basically its all about return values and evaluations.
You will leak memory if you do this as you are stating and don't clean it up afterwards.
A better way would be to use a shared_ptr.
The code would look like this
cursors.push_back(std::make_shared<cursor>(4));
It's a little hard to understand exactly what you're trying to accomplish though.
As mentioned below in the comments by #cat-plus-plus, unique_ptr should be used unless you explicitly want the object shared elsewhere, the code then would look like the following:
cursors.push_back(std::unique_ptr<cursor>(new cursor(4)));
It will work, but chances are pretty good that you're better off doing something else. Absent a really good reason to do otherwise, I'd consider making it a vector of cursors (instead of pointers to cursors). In this case, you can just do something like:
std::vector<cursor> cursors;
cursors.push_back(4); // or cursors.emplace_back(4);
This generally improves programmer efficiency by automating memory management, and improves code efficiency by eliminating unnecessary levels of indirection.

C++: More and more complex bugs misleading the debugger [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
It seems like the bugs in my project are evolving. While in the beginning time, causers of program crashes were easily to spot using the debugger (the line it points to after the program crashes), it's different now.
Many bugs cause the program to crash in completely arbitary places not even closely related to them.
How can I spot bugs "hiding" from the debugger in a better way?
(I use Visual Studio 2010)
Profile your code for memory corruption, memory overwrites and memory leaks.
Root cause analysis.
When you find an obvious bug, don't just fix the bug, fix the coding style that allowed it.
If you have any code using raw memory and pointers, replace it by memory allocated using std::vector and its iterators. It will compile to exactly the same speedy code in Release mode, but in Debug mode it will used checked iterators, which will catch some bugs as early as possible.
Use memory corruption checking utilities, like gflags or debug heap. "Floating" crushes almost always come from corrupted memory in C++ programs

Finding a totally nasty, complex, Schröding-Bohr-Bug [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a really nasty bug in my program, which grew quite complex over time. It's probably the worst bug I've ever had.
I think that it might be related to a static variable initialization fiasco, but how can I ensure myself of that?
When the bug strikes, the program crashes due to heap corruption at a random point after startup, but far inside the main() function.
To be honest, I don't know what to do.
I'm on Windows 7 using Microsoft Visual Studio 2010
my program, which grew quite complex
over time
Do you keep backups of previous versions?
Find an older version that worked and continue working based on that version...
There is a famous quote out there:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan
If this program has become more complicated than you can handle then it may be time to think about refactoring.
(This is in no way intended to be demeaning or to be taken as a personal attack...)
Run your program in the debugger, and step through the code until you see what's wrong. Place breakpoints liberally anywhere you think the bug might be caused.
Try debugging your program with gdb.

FFmpeg potential mem leak like behavior points. Are there such? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
So I mean like it tries to educate it self and collects some data... Are there any such points/feilds in encoding part of ffmpeg (that I hope can be disabeld)?
BTW: My problem is simple: I looked thrue out all my code. It seriosly looks like it is some part of my ffmpeg windows build leaks memory a littel... all the time while I am encoding... So I hope ffmpeg is just triing to learn so that I would be able to tell ti not to learn!)
FFmpeg libraries use a very object-oriented design. All memory allocated should be kept track of in the context structures and freed when the relevant context is destroyed. There may be some one-time allocation and initialization of constant global data which one could call a "leak", but I believe that was all replaced with static const tables to facilitate better use of shared memory and eliminate memory leaks associated with dynamic loading. If you really think it's leaking (and if you care), you need to use some memory debugging tools to identify where the leaks might be and coordinate finding/fixing them with the developers.
If what you mean is that during a single encode, memory usage grows slightly, this is probably normal and to be expected. It shouldn't be much, and the memory should all be released when the encoding context is freed.