-4

I have a simple code about managing resource by using vector. But when i run to this line :

sentClientList.push_back(socket);

It randomly break with this exeption throw : A heap has been corrupted (parameters: 0x76F9D8D0). What i noticed here is the address is triggered always the same for every time. Here is the bug enter image description here

I try to debug but it occurred when ever it wants and just one every time I run the program. But when i press continue, the first value in vector become normal, it just ignores the bug and runs probably.

So here are the questions:

  1. What way I can ruin the data in vector without touching it?
  2. Is there anyway to automatically ignore this error?
  3. Can I use try catch to catch this?
Bart van Nierop
  • 4,130
  • 2
  • 28
  • 32
  • 2
    The cause of the crash is elsewhere. Perhaps a faulty copy constructor, or errant vector index access. – Bathsheba Dec 23 '16 at 16:09
  • What's the definition of the vector? (Why type is held in it?) – ebyrob Dec 23 '16 at 16:09
  • 3
    There isn't nearly enough to go on here. Please supply a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Bart van Nierop Dec 23 '16 at 16:09
  • it just held a SOCKET. – user3819845 Dec 23 '16 at 16:10
  • 1
    @user3819845 You are just pointing out where your program finally breaks down. The origin of the problem is elsewhere in your code. Either that, or `sentClientList` is an invalid instance and you're accessing the vector using this instance. That's why you need to post an [mcve], so that we know in what context this function is being called. – PaulMcKenzie Dec 23 '16 at 16:15

1 Answers1

4
  1. What way I can ruin the data in vector without touching it?

E.g. corrupting the stack the std::vector variable was created with. Think about if any data (e.g. the pointer to manage that vector) is overwritten accidentally. The vector variable is screwed up and cannot be used reasonably anymore.

  1. Is there anyway to automatically ignore this error?

No, your complete program is screwed up at that point (as mentioned). You cannot ignore that.

Any step further is undefined behavior, which can theoretically result in

  • Your Schrödinger cat dies (or not)
  • Your fridge explodes (because of weird IoT feedbacks triggered)
  • Little demons are flying out of your nostrils
  • Time travel
  • ...
  1. Can I use try catch to catch this?

No, you can't. That's nothing left to exception handling.

Though there are tools like valgrind or equivalent tools for Visual Studio, to help debugging the source of corruption.


Sorry for the answer on an off-topic question, but that was too big for a comment.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks. But why it can become "normal" when i press continue? – user3819845 Dec 23 '16 at 16:15
  • @user3819845 Define _"normal"_ please. Sounds like you're fishing in the realms of _undefined behavior_. – πάντα ῥεῖ Dec 23 '16 at 16:16
  • 2
    @user3819845 -- Don't press "continue" and actually fix the bug. Pressing "continue" is fools gold. – PaulMcKenzie Dec 23 '16 at 16:16
  • Thanks. I mean pressing continue some how can makes the program runs without error. This function is called continuously. When it breaks down, I dont have any clue to fix this then try to continue it. – user3819845 Dec 23 '16 at 16:22
  • 1
    @user3819845 Sure, with enough luck and all the stars aligning, pressing "continue" instead of fixing the bug will run the program correctly for a while, until it breaks down again. If you don't know how to fix the bug, then give us more information to work with than just a memory address. – Bart van Nierop Dec 23 '16 at 16:25