-1

I developed a project on Mac using C++. It works perfectly. However when I try to launch it from a Linux server, I get a bad alloc error:

terminate called after throwing an instance of 'std::bad_alloc' 
what():    std::bad_alloc 
Aborted

I don't know how to debug the whole project because I have absolutely no idea of where the issue come from. Why is it working on my Mac and not on Linux? All articles and questions about that only ask for single file program but not 40+ files project.

Is there a way to get the file or line that causes the bad alloc?

Paul Bénéteau
  • 775
  • 2
  • 12
  • 36
  • 1
    Why don't Google "Debug huge C++ project on linux" literally? – 273K Feb 22 '20 at 17:02
  • 3
    You are looking for a *debugger*. For Linux, `gdb` is standard. When you run your program in `gdb`, it will stop at the place where exception is thrown and show you whole stack trace which led to that exception. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Yksisarvinen Feb 22 '20 at 17:03
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Feb 22 '20 at 17:03
  • 2
    "Is there a way to get the file or line that causes the bad alloc?" - of course there is. Load the core dump file, that was generated when the program crashed, into your debugger and inspect the call stack. Or just run it directly in the debugger in the first place. – Jesper Juhl Feb 22 '20 at 17:05
  • @S.M I did it and found nothing. – Paul Bénéteau Feb 22 '20 at 17:45
  • 1
    In linux you have a great tool, Valgrind, very useful in such cases. Generally it directly show the line where such problems occur – Damien Feb 22 '20 at 22:42

1 Answers1

0

Build your program with the -g compiler option to get meaningful stack traces.

Then run the program in a debugger, e.g. gdb:

gdb --args [executable] [arguments...]

When gdb has loaded, enter the command run and you program will be run. As soon as the exception is thrown and not caught by your program, gdb will show you a stack trace showing you from where the exception was thrown.

std::bad_alloc means that your program wasn't able to allocate any more memory, probably because the OS either ran out of memory or you hit an explicit memory limit imposed by the system.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • I have multiple .cc and .h files including a main.cc, how do I create the executable with gcc? – Paul Bénéteau Feb 22 '20 at 17:44
  • 1
    @PaulBénéteau How did you manage to compile it before you ran it?? – walnut Feb 22 '20 at 17:48
  • I used Xcode. I found the line that causes the issue using a lot of prints. I create input_ = std::make_shared() and I load the content of a file in it: input_->load(in). It's there that occurs the bad_alloc I think. – Paul Bénéteau Feb 22 '20 at 18:00
  • @PaulBénéteau You generally cannot use the program compiled for one system on another one. You cannot copy the executable from a Mac to a Linux system and assume that it will run correctly. – walnut Feb 22 '20 at 18:01
  • I do not copy the executable but all the source files. – Paul Bénéteau Feb 22 '20 at 18:02
  • @PaulBénéteau ok, but how did you compile those into an exectuable? – walnut Feb 22 '20 at 18:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208330/discussion-between-paul-beneteau-and-walnut). – Paul Bénéteau Feb 22 '20 at 18:20