c++ - Access violation on ending the main function scope but only with Visual Studio 2019 and gcc - Stack Overflow
I don't understand why I'm getting the access violation on reading location
on leaving the main function scope in this program:
#include <iostream>
#include <string>
struct SimpleStructBase
{
int* intPointer;
};
struct MemHolder
{
SimpleStructBase& params;
MemHolder(SimpleStructBase& dist) : params(dist)
{
params.intPointer = new int(*dist.intPointer);
}
~MemHolder()
{
delete params.intPointer;
}
};
struct SimpleStructMng : SimpleStructBase
{
protected:
SimpleStructMng(SimpleStructBase& params) : SimpleStructBase(params) {}
std::shared_ptr<MemHolder> mem;
public:
~SimpleStructMng() {}
static SimpleStructMng CreateStruct(SimpleStructBase& params)
{
SimpleStructMng ret = { params };
ret.mem = std::make_shared<MemHolder>(ret); //This thing crashes
return ret;
}
};
int main() {
SimpleStructBase objBase;
objBase.intPointer = new int(42);
SimpleStructMng objMng = SimpleStructMng::CreateStruct(objBase);
std::cout << "Base object: " << *objBase.intPointer << "\n";
std::cout << "Managed object: " << *objMng.intPointer << "\n";
return 0;
}
In the end of the CreateStruct
, MSVC 2019 compiler (as well as gcc) for some reason first clears the object ret
(also removing the only reference in ret.mem
smart pointer) and then returns the object from the function. Of course, ret.mem
becomes unavailable. Interesting that MSVC 2022 doesn't do that and everything works fine.
How can I change the program so it doesn't crash even in MSVC 2019/gcc? The only restriction is that the SimpleStructBase
structure should keep unchanged.
最新文章
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- python - How to add a linear gradient to a QTableWidget? - Stack Overflow
- Django is not updating on MacOS - Stack Overflow
- c++ - GLOG - flags.cc' is being linked both statically and dynamically into this executable - Stack Overflow
- java - Handling order Id In OMS system on application level - Stack Overflow
- node.js - Capture PID of sub-command in script for use to terminate that sub-command when exiting with Ctrl-C - Stack Overflow
- javascript - Why does every object in BabylonJS require a name? - Stack Overflow
- matplotlib - Annotate each subplot from a list - Stack Overflow
- pytorch - how to get custom column in the model's forward() function when training with Huggingface Trainer? - Stack Ove
- c# - Why is OntriggerEnter2D not working on my Flappy Bird game?Or if not what is the problem and can you solve it? - Stack Over
- assembly - Why could not I apply call gate successfully? - Stack Overflow
- visual studio code - VSCode support fully integrated IPython terminal - Stack Overflow
- python - Galaga game not running due to an empty range - Stack Overflow
- php - The update and destroy parameters cannot be used in CRUD Laravel 11 - Stack Overflow
- scipy - Problem with a simple script in which the Librosa Python library does not work well for me - Stack Overflow
- ios - Persist overlay view in the detail side of NavigationSplitView - Stack Overflow
- reactjs - How to render text and list from markdown frontmatter on Next project? - Stack Overflow