boost::interprocess::managed_windows_shared_memory crashes on VC11 - c++

I have the following simple program:
#include <boost\interprocess\managed_windows_shared_memory.hpp>
#include <boost\interprocess\shared_memory_object.hpp>
using namespace boost::interprocess;
int main ()
{
managed_windows_shared_memory segment(create_only, "MySharedMemory", 655360);
return 0;
}
It crashes with the following exception:
Unhandled exception at 0x00007FF6B7741664 in Server.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
This happens only for the Release build in VC11. (VC10 is fine). Looking at the call stack, it is happening at rebalance_after_insertion() in boost\intrusive\rbtree_algorithms.hpp. Did anyone face the similar issue?

If anyone would encounter this, the problem seems to be code generation-related with a specific version of boost (in my case v 1.55.0). It also only happens in a release-configuration and when applying optimization (i.e. '/O1', '/O2' or '/Ox'). Using Boost v.1.58 solved the problem.

Related

VS Code: Declaring a stack leads to problems with cout

I am learning C++ and was writing a simple program when I noticed that declaring a stack caused cout to not output to the terminal. Here is my program:
#include <iostream>
#include <stack>
int main ()
{
std::stack<int> myStack;
std::cout<<"Hello World!"<<std::endl;
return 0;
}
This problem only occurs in the Visual Studio Code editor. If I use an online C++ compiler, there are no issues and I see "Hello World!" as output. If I remove the declaration of the stack, the output is also normal, even in VS Code. However, when the declaration is present, I see no output when I compile and run my code in VS Code. I have no idea why this would be the case, and other STL containers (I've tested vector, array, and set) do not cause a similar issue, although the problem still occurs when queue is used.
Any help resolving this issue, or any insight as to why it occurs in the first place, would be greatly appreciated.
p.s. This is my first time asking a question, so I apologize if it is poorly written.

CPP console applications exits without providing error information

When I compile and run the below piece of code the exe crashes yet doesn't provide information of any sort regarding why it crashed. (Seg faults etc. not reported). Here is the sample code that I tried:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <int> values;
int temp = values.back();
cout << temp << endl;
return 0;
}
The same piece of code when compiled and run-on Linux produce a seg fault. Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?
Calling std::vector::back on an empty vector is undefined behavior. The compiler or runtime aren't required to emit a diagnostic. There might be a tool or sanitizer that could help depending on the compiler you're using.
Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?
The problem is that value.back() causes undefined behavior (because your vector is empty). The effect of undefined behavior is undefined. It might result in a seg fault, but also could just exit the application. You can't enforce a segfault for that, and not really enforce a crash.
Using static analyzers and increasing the warning level can help in certain cases. But there is no way to detect all possible errors.

Segmentation error with caffe

I am using caffe on Windows, and am getting segmentation errors which I cannot pinpoint. It happens when the program exits, and WinDbg said scalar deleting destructor, no idea where the memory was allocated. My complete code (currently a dummy code trying to narrow it down, but it happens only sometimes):
#include <string>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "google/protobuf/text_format.h"
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"
using caffe::Blob;
using caffe::Caffe;
using caffe::Datum;
using caffe::Net;
using std::string;
namespace db = caffe::db;
int main(int argc, char** argv) {
// Initialize logging with program call. First thing that needs to be done
::google::InitGoogleLogging(argv[0]);
cv::Mat mean_;
// Set Caffe to run in CPU only mode
Caffe::set_mode(caffe::Caffe::CPU);
std::vector<std::string> labels_;
/*std::shared_ptr<Net<float>> caffe_test_net;*/
Net<float>* caffe_test_net;
caffe_test_net = new Net<float>("D:\\Development\\caffe-windows\\models\\bvlc_reference_caffenet\\deploy.prototxt", caffe::Phase::TEST);
caffe_test_net->CopyTrainedLayersFrom("D:\\Development\\caffe-windows\\models\\bvlc_reference_caffenet\\bvlc_reference_caffenet.caffemodel");
delete caffe_test_net;
return 1;
}
I have tested with caffe_net in a unique or shared_ptr, but that made no difference at all. I am at a loss on how to find the issue at hand.
"Happens sometimes" is a pretty common thing with undefined behavior, which is what you're really encountering. A segmentation fault is one of a theoretically infinite number of things that the computer might do - the behavior is literally undefined. In other words, as they say on USENET: "It is legal for the compiler to make demons fly out of your nose." It may work, it might do something strange, or it might throw some major error like a segfault.
There are tools dedicated specifically to tracking down segmentation faults and other memory errors. On Linux, that's generally Valgrind, while on Windows, you'd use Dr. Memory. So long as you compiled with the debugging symbols included (-g), when you run the executable through Dr. Memory, it should give you a stack trace for the segmentation fault.
As soon as you get the stack trace, check the top of it to see which destructor the code is whining about, or in the very least, what line of code in main.cpp is calling the function(s) responsible for the undefined behavior.
Also, depending on your compiler, you may be encountering a known bug in VC.
You can find more general information about segmentation faults, common causes, and how to debug them on this answer.

Strange Exceptions using RapidXml under Windows CE 6.0/Windows Mobile/Windows Embedded Compact

I'm having a very strange problem when trying to run RapidXml 1.13 under Windows CE 6.0 compiled with Visual Studio 2005. I have an extremely small program that fails to run:
#include <rapidxml.hpp>
using namespace rapidxml;
int _tmain(int argc, _TCHAR* argv[])
{
xml_document<char> doc;
return 0;
}
It compiles fine with 0 errors and 0 warnings (at W3). However, when I run or debug the program, I get an access violation exception:
First-chance exception at 0x000110d4 in RapidXml_Test.exe:
0xC0000005: Access violation writing location 0x0001fb48.
The debugger then points to this line (1366 in rapidxml.hpp) as the culprit (the open brace):
template<class Ch = char>
class xml_document: public xml_node<Ch>, public memory_pool<Ch>
{
public:
//! Constructs empty XML document
xml_document()
: xml_node<Ch>(node_document)
------->{
}
...
If anyone has any clue what the problem could be I'd greatly appreciate it. I have much more complicated code working in my build and run-time environment so I don't suspect anything there. I'm also fairly confident it's not a project setting. I assume at this point that RapidXml's use of templates is somehow confusing the Windows CE VC++ compiler. I don't know what else it could be.
Thanks in advance!
I found the solution. RapidXML allocates its own pool of memory once its loaded. Problem is, I think it allocates it on the stack and I was getting a stack overflow! (How serendipitous that the problem with my first question here actually WAS a stack overflow). Anyways, reducing the size of the pool solved my problem.

Program crashing on exit

Whenever I exit my program it gives me this exception "0xC0000022: A process has requested access to an object, but has not been granted those access rights."
It breaks right at the end of a function called _lock_file in _file.c.
After trying to narrow down what the cause of the problem is I found out that it does not crash if I remove all fclose() function calls in my program then cleaning and rebuilding my program. Even if the function itself is never called it will still crash. Obviously this solution is not ideal.
When I tried to use fstream instead it produced a similar crash at the start of the program.
It's also worth mentioning that my program uses SDL.
Edit: Someone requested a minimal example and this is what I cam up with.
main.cpp
#include <stdlib.h>
#include <SDL.h>
/*...*/
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
/*...*/
int main( int argc, char **argv)
{
if(false)
fclose(NULL);
return 0;
}
draw.cpp
/*...*/
If I run this it will crash on exit just like I mentioned above. And yes the draw.cpp is completely commented out, but if I remove it from the project the program will run fine. All other files were removed from the project.
Edit2: In response to karlphillip I decided to double check if it is actually running and it seems that it is actually crashing at the start with this example.
Also it is a Win32 project.
Having a crash on exit usually means that the heap is corrupted during program execution. Try using a memory checker to find where. Try using _CrtDumpMemoryLeaks()
Are you using the same runtime library (Debug DLL, Debug, Release DLL, Release, etc.) for your main program as was used to build the SDL library? That can often (but not always) cause odd problems, and would be my first port of call when getting this sort of odd behaviour at runtime.
(If you get an LNK4098 warning when building, this is what it is trying to tell you, and you really need to fix it properly; the "solution" the text of the warning suggests is anything but.)
Another option is memory corruption. Consider running a debug build, and calling the following on startup:
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|_CRTDBG_CHECK_ALWAYS_DF);
This activates more thorough heap checking. (You might have to go and make a cup of tea when your program runs with this switched on, if it allocates lots of stuff while it's running.) If then "crashes" in one of the memory allocation functions -- it's actually an assert, you can't always tell though -- then at some point between that call, and the previous call to a memory management function, something has overwritten some memory it should not have. And you can take it from there, to find out what.
-Edit: "_CRTDBG_REPORT_FLAG_DF", was probably intended to be "_CRTDBG_REPORT_FLAG".
Crashing on exit can also be caused by static variables destructing and accessing objects that have already been cleaned up.
Check you static objects and ensure their destructors are not causing the crash.
How do you know your application is being executed in the first place? Add a debug right after main() is called:
#include <stdlib.h>
#include <SDL.h>
/*...*/
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
/*...*/
int main( int argc, char **argv)
{
printf("dbg1\n");
if(false)
fclose(NULL);
printf("dbg2\n");
return 0;
}
What kind of project are you creating? Console, Win32 or something else?
I find this post very interesting.