0

I am using a large code-base, so forgive me for not including all source code. I will summarize the problem as best I can and hopefully it is enough to give some intuition into an answer.

When creating a pointer to a pointer (used as a dynamic array of pointers), I get an error somewhere in the heap, see image below for call-stack:

Error

all setFrontNodes() is:

void Node::setFrontNodes(int size) {
    frontNodes = new Node*[size]; // ERROR ON THIS LINE
    nFrontNodes = size;
}

Where the header for my class Node is:

class Node {
public:
    ~Node();
    int nBackNodes;
    int nFrontNodes;
    Node** backNodes;
    Node** frontNodes;
    void setFrontNodes(int size);
    void setBackNodes(int size);
    double value;
    double valuePrime;
    ActivationFunction* activationFunction = NULL;
    InitWeightMethod* initWeightMethod = NULL;
    void initWeights(double multiplier);
    double bias;
    double deltaBias;
    double* weights;
    double* deltaWeights;
    double errorGradient;
    Node(int number);
    void forwardProp();
    int number;
    string label;
    int layer;
};

Now here is the strange issue. The error will happen randomly. I will run the same program with same parameters and everything, and the error will either happen there, or at another time while running, or not at all! This makes it extremely difficult to track the bug down and why I do not have a full code example that can be repeated.

It sounds like there is some issue allocating memory on the heap, though I am not sure what it could be or how to fix it. Vectors do not cause errors but are mind numbingly slow when training neural networks such as I am using the code for.

I ran memory diagnostics, and I still have over 4gb free of RAM when the error happens.

Thanks for any time! Let me know if you want anything else, but like I said it is a large program and the error appears random so I am looking for a general problem that I might be running into when allocating memory on the heap which is causing the ntdll.dll!_RtlReportCriticalFailure@12() error in the screenshot.

3
  • 1
    I see two possibilities. Either size is a garbage, very large value (possibly uninitialized variable); or your program is corrupting the heap elsewhere, in the code not shown. Commented Apr 12, 2019 at 3:44
  • I updated with a new screen shot, size is only 200, so that is not the problem. So i must be corrupting the heap elsewhere, do you have any references or advice on this? I am a self-taught programmer, so ignorant to some things. Commented Apr 12, 2019 at 3:59
  • once you fix your code, it may run just as slowly as the vector implementation. consider investing time understanding the vector performance issue instead trying to maintain this fragile structure. Commented Apr 12, 2019 at 4:37

1 Answer 1

1

As commented by Igor Tandetnik, this sort of error is likely due either to a garbage value for size, or due to the heap being corrupted. Make sure size is a valid number, and make sure that you're not writing to memory you're not supposed to touch (as this could corrupt the heap).

If possible, you can use a code sanitizer (like clang) to check for this, or you could use a tool like valgrind to see when it happens.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for the answer, I will check out clang and valgrind. I updated the question with a new screenshot that is more informative, does this help at all? It does shows that size=200 so that is not the issue, but what about the block address?
In this case, the place where the error occurs is not the place where the code is broken. The code breaks prior to that point, most likely due to heap corruption, and it would require going through a lot more code to understand where the heap corruption occurs.
Tools like valgrind and address sanitizers can check for things like heap corruption, and pinpoint where it occurs.
Your program has data structures that keep track of allocated memory. This is the heap. When those data structures get overwritten (usually because of a bad write or a bug in the code), that’s what causes heap corruption.
hmm I working on tracing down the bug, thanks now I know (kind-of) what to look for! I'd like to find it myself rather than using a program because I might learning something.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.