3

In my C++ project, I encounter a very strange issue. It crashes with exit code 11 when I remove a certain log statement (cout).

This answer points to a source that explains exit code 11 (actually EAGAIN) with the following statement:

The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.

But I am pretty sure don't create any additional threads in my code (at least not explicitly). So why does the error occur and why does it go away when I use the log statement?

For reference, I will post the code but it's of course completely out of context and basically the only relevant line is the one with the log statement.

 PayloadRegionMapper(string mappingTechniqueName, string configPath = "")
    : payload(PAYLOAD), config(Config(configPath)) {

    cout << "construct PayloadRegionMapper" << endl; // if commented out, my program crashes....

    frames = generateFrames();
    setMappingTechnique(mappingTechniqueName);
}
Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132
  • What do you get when you attach a debugger? – Nick Banks Apr 22 '15 at 15:47
  • run a memory checker, like valgrind – BЈовић Apr 22 '15 at 15:47
  • 1
    Did you try replacing the `cout` with another statement? And is that a `std::cout`? – Carcigenicate Apr 22 '15 at 15:47
  • Is this code snipped run in a concurring thread? It seems like a race condition. – HappyCactus Apr 22 '15 at 15:48
  • Sounds like there is some UB in your code – NathanOliver Apr 22 '15 at 15:49
  • @Carcigenicate yes it's `std::out`... and I just replaced the statement and used another (random) one (`int a = 4`)... that indeed also prevents a crash... – nburk Apr 22 '15 at 15:49
  • 7
    most likely you have Undefined Behavior in your code, and the compiler behaves differently depending on whether that statement exists or not. The cout is not the problem. – bolov Apr 22 '15 at 15:50
  • @nburk That's very odd. From just the snippet, I have no clue, but it's also only a fairly narrow window into your program. – Carcigenicate Apr 22 '15 at 15:54
  • hmm.. yeah I don't think posting any more code will help much. I'm quite a novice to C++ so I guess I'll first go and do some reading on UB and maybe find strategies to resolve them (and reinspect my code for that matter...). thanks for the hints to everyone! – nburk Apr 22 '15 at 15:57
  • 1
    Try removing the cout, then taking your remaining two functions calls (generateFrames and setMappingTechnique) and commenting them out one at a time. If the call to setMappingTechnique requires generateFrames to have done something, you may need to add a minimal amount of filler code to get it to run. Generally you want to start trying to eliminate other things that are running one at a time and see if it resolves the issue. – RyanP Apr 22 '15 at 16:10
  • Try to replace cout with a Sleep(100) and see if it still fails. If not, then you are most likely has a concurrency issue, otherwise I would guess it is a memory allocation/access problem. – mg30rg Apr 23 '15 at 08:29

1 Answers1

0

Run the program using a debugger and then backtrace once the crash happens. Using the bt and frame command you can get an idea about the behaviour of the program during the crashing situation.

gdb <executable>

.....<crash happened>

bt
<It will give you the stack frame >

frame <frame number>

Then look for the values and memory area there. 
Rndp13
  • 1,094
  • 1
  • 21
  • 35