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.
最新文章
- 德国考虑立法强迫IT公司公布软件源代码
- 50公里的传奇 Ubiquiti Networks
- Power apps - Multiple Sharepoint lists combine as collection and display in one gallery - Stack Overflow
- Why has VS Code stopped discovering my python tests? - Stack Overflow
- ios - Flutter: works on Xcode but not on VS code and Android Studio - Stack Overflow
- for loop - CSS masonry grid with dynamic rows width - Stack Overflow
- swift - How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow
- html - Disable scrolling. Overflow:hidden not working - Stack Overflow
- sql - Ranking query records in specific order - Stack Overflow
- python - Pyinstaller (Mac App) - Why only permission given to the executable file (instead of .app bundle) could work? - Stack O
- Meta Graph API Post engagement - Stack Overflow
- c# - Make custom file property columns show by default in Windows File Explorer - Stack Overflow
- javascript - How to properly handle AES encryption in React Native and generate Random Key for AES encryption? - Stack Overflow
- Font Size Mismatch When Using Fallback Fonts in FFmpeg ASS Subtitles - Stack Overflow
- azure - Unable to get a token to create B2C user using Graph API - Stack Overflow
- VS Code : How to deactivate matching characters in bold? - Stack Overflow
- Is there showhide functionality for boiler plate content in google docs? - Stack Overflow