Of Memory Management, Heap Corruption, and also C+npls

So, I require some aid. I am working with a task in C+npls. Nonetheless, I assume I have actually in some way taken care of to corrupt my heap. This is based upon the reality that I included an std::string to a class and also appointing it a value from an additional std::string:

std::string hello = "Hello, world.\n";
/* exampleString = "Hello, world.\n" would work fine. */
exampleString = hello;

collisions on my system with a stack dump. So primarily I require to stop and also experience all my code and also memory monitoring things and also figure out where I've messed up. The codebase is still tiny (concerning 1000 lines), so this is conveniently do - able.

Still, I'm over my head with this sort of things, so I assumed I would certainly toss it around. I'm on a Linux system and also have actually jabbed about with valgrind, and also while not recognizing entirely what I'm doing, it did record that the std::string is destructor was a void free. I need to confess to obtaining the term 'Heap Corruption' from a Google look ; any kind of basic objective write-ups on this type of things would certainly be valued too.

(In prior to rm -rf ProjectDir, do once more in C#:D)

EDIT: I have not made it clear, yet what I'm requesting for are means a suggestions of detecting these type of memory troubles. I recognize the sexually transmitted disease:: string things is right, so it is something I've done (or a bug, yet there is Not A Problem With Select). I'm certain I can examine the code I've written and also you really clever individuals would certainly see the trouble in a snap, yet I intend to add this sort of code evaluation to my 'tool kit', as it were.

2019-05-18 22:13:18
Source Share
Answers: 10

As much as I can inform your code is proper. Thinking exampleString is a sexually transmitted disease:: string that has class extent like you define, you should certainly have the ability to initialize/assign it in this way. Probably there is a few other concern? Possibly a fragment of real code would certainly aid place it in context.

Inquiry: Is exampleString a reminder to a string object developed with new?

2019-05-21 06:31:22

It can be heap corruption, yet it is equally as most likely to be stack corruption. Jim is right. We actually require a little bit extra context. Those 2 lines of resource do not inform us a lot alone. There can be any kind of variety of points creating this (which is the actual pleasure of C/C+npls ).

If you fit uploading your code, you can also toss all of it up on a web server and also upload a link. I'm certain you would certainly obtains whole lots extra suggestions in this way (several of it most certainly unconnected to your inquiry).

2019-05-21 06:30:17

The code was merely an instance of where my program was falling short (it was alloted on the stack, Jim). I'm not in fact seeking 'what have I done incorrect', yet instead 'just how do I detect what I've done incorrect'. Educate a male to fish and also all that. Though considering the inquiry, I have not made that clear adequate. Give thanks to benefits for the edit function.:'-RRB- Also

, I in fact dealt with the sexually transmitted disease:: string trouble. Just how? By changing it with a vector, assembling, after that changing the string once more. It was continually collapsing there, which dealt with despite the fact that it ... could not. There is something unpleasant there, and also I'm not exactly sure what. I did intend to examine the one-time I by hand allocate memory on the heap, though:

 this->map = new Area*[largestY + 1];
 for (int i = 0; i < largestY + 1; i++) {
     this->map[i] = new Area[largestX + 1];

and also removing it:

for (int i = 0; i < largestY + 1; i++) {
    delete [] this->map[i];
delete [] this->map;

I have not alloted a 2d array with C+npls prior to. It appears to function.

2019-05-21 06:16:48

Oh, if you need to know just how to debug the trouble, that is straightforward. First, get a dead hen. After that, start shaking it.

Seriously, I have not located a regular means to track these sort of pests down. Due to the fact that there is numerous possible troubles, there is not a straightforward list to experience. Nonetheless, I would certainly advise the following:

  1. Get comfy in a debugger.
  2. Start tromping about in the debugger to see if you can locate anything that looks dubious. Examine specifically to see what is taking place throughout the exampleString = hello; line.
  3. Examine to see to it it is in fact collapsing on the exampleString = hello; line, and also not when leaving some confining block (which can create destructors to fire).
  4. Examine any kind of reminder magic you could be doing. Reminder math, spreading, etc
  5. Check every one of your appropriations and also deallocations to see to it they are matched (no double - deallocations).
  6. See to it you aren't returning any kind of referrals or reminders to things on the stack.

There are great deals of various other points to attempt, also. I'm certain a few other individuals will certainly chip in with suggestions too.

2019-05-21 06:15:51

Also, I in fact dealt with the sexually transmitted disease:: string trouble. Just how? By changing it with a vector, assembling, after that changing the string once more. It was continually collapsing there, which dealt with despite the fact that it ... could not. There is something unpleasant there, and also I'm not exactly sure what.

That seems like you actually did drink a hen at it. If you do not recognize why it is functioning currently, after that it is still damaged, and also virtually assured to attack you once more later on (after you've included a lot more intricacy).

2019-05-21 06:14:45

Some areas to start:

If you get on windows, and also making use of aesthetic C+npls 6 (I want to god no one still utilizes it nowadays) it is implentation of sexually transmitted disease:: string is not threadsafe, and also can bring about this example.

Here's an article I found which explains a lot of the common causes of memory leaks and corruption.

At my previous office we made use of Compuware Boundschecker to aid with this. It is business and also really pricey, so might not be an alternative.

Below is a number of free collections which might be of some usage



Hope that aids. Memory corruption is a sucky area to be in!

2019-05-21 06:12:29

Run Purify.

It is a close to - enchanting device that will certainly report when you are belting memory you should not be touching, dripping memory by not releasing points, double - releasing, etc

It operates at the equipment code degree, so you do not also need to have the resource code.

Among one of the most delightful supplier teleconference I was ever before on was when Purify located a memory leakage in their code, and also we had the ability to ask, "is it feasible you are not releasing memory in your function foo ()" and also listen to the awe in their voices.

They assumed we were debugging gods yet after that we allowed them know the key so they can run Purify prior to we needed to utilize their code.: -)


(It is rather costly yet they have a free eval download)

2019-05-21 06:10:41

Your code as I can see has no mistakes. As has actually been claimed extra context is required.

If you have not currently attempted, install gdb (the gcc debugger) and also compile the program with - g. This will certainly compile in debugging icons which gdb can make use of. As soon as you have actually gdb mounted run it with the program (gdb ). This is a valuable cheatsheat for making use of gdb.

Set a breakpoint for the function that is generating the bug, and also see what the value of exampleString is. Additionally do the very same for whatever parameter you are passing to exampleString. This need to at the very least inform you if the sexually transmitted disease:: strings stand.

I located the solution from this article to be an excellent overview concerning reminders.

2019-05-21 05:57:58

One of the debugging strategies that I make use of regularly (other than in instances of one of the most severe quirkiness) is to separate and also overcome. If your program presently falls short with some details mistake, after that separate it in fifty percent somehow and also see if it still has the very same mistake. Clearly the method is to determine where to separate your program!

Your instance as offered does not show adequate context to establish where the mistake could be. If any person else were to attempt your instance, it would certainly function penalty. So, in your program, attempt getting rid of as much of the added things you really did not show us and also see if it functions after that. If so, after that add the various other code back in a little bit at once till it begins falling short. After that, things you simply included is possibly the trouble.

Keep in mind that if your program is multithreaded, after that you possibly have bigger troubles. Otherwise, after that you need to have the ability to tighten it down this way. All the best!

2019-05-19 10:33:51

These are reasonably economical devices for perhaps addressing the trouble:

  1. Keep an eye on my heap corruption question - I'm upgrading with the solutions as they clean. The first was stabilizing new[] and also delete[], yet you are currently doing that.
  2. Offer valgrind even more of a go ; it is a superb device, and also I just desire it was readily available under Windows. I just reduces your program down by concerning fifty percent, which is respectable contrasted to the Windows matchings.
  3. Think of making use of the Google Performance Tools as a substitute malloc/new.
  4. Have you cleaned all your object files and also began again? Probably your make documents is ... "suboptimal"
  5. You are not assert()ing sufficient in your code. Just how do I recognize that without having seen it? Like flossing, no - one assert()s sufficient in their code. Include a recognition function for your things and also call that on method start and also method end.
  6. Are you compiling -wall? Otherwise, do so.
  7. Locate on your own a dust device like PC-Lint. A tiny application like your own could suit the PC-lint demo web page, suggesting no acquisition for you!
  8. Examine you are NULLing out reminders after deleteing them. No one suches as a dangling reminder. Very same job with proclaimed yet unallocated reminders.
  9. Stop making use of selections. Make use of a vector rather.
  10. Do not make use of raw reminders. Make use of a smart pointer. Do not make use of auto_ptr! That point is ... shocking ; its semiotics are really weird. Rather, pick among the Boost smart pointers, or something out of the Loki library.
2019-05-19 08:50:35