T
T
Tesla4o2019-09-16 10:50:24
C++ / C#
Tesla4o, 2019-09-16 10:50:24

Why can't a constructor be created?

There are two classes, for example A and B. One class calls the constructor of the other.
Example:

class A {
    A() {
        (new B());
    }
}

In Linux, everything is created without problems, but in Windows 7 it is assembled without errors, but when executed, it crashes with the error c0000005. It reaches the call of this constructor and does not even enter it and falls.
I understand this is a problem with memory allocation?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Adamos, 2019-09-16
@Adamos

(new B());

Memory is allocated for an instance of class B, its constructor is launched, the reference returned by the constructor is not used anywhere, the destructor is never called.
Just an academic memory leak.
Judging by this shitty code, the cause of the error can be anywhere in other code.

A
Antony, 2019-09-16
@RiseOfDeath

"Wonderful" code. Only before spreading, it would be good to make sure that this error is reproduced on it.
Make the smallest possible code where you would have a problem that you are asking for help solving. It is likely that in the process of creating one you will suddenly find your mistake (maybe you, for example, messed up the call stack somewhere before calling the constructor).
And if you make a simple example where an error occurs, it doesn’t work - then you don’t understand correctly where it occurs and it’s probably somewhere in the “heap” that cannot be thrown out of the example.

V
vanyamba-electronics, 2019-09-16
@vanyamba-electronics

Try like this

class A {
public:
    A() {}
    static A* create(A* val = nullptr) {
       if (val == nullptr)
            val = new A();
       new B;
       return val;
     }
};
...
A* a = A::create();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question