-2

I'm using Qt on Ubuntu and doing a research test that needs many executions of my program. I decided to use the thread to optimise the time consumption, but I had a problem when the parameters are large. First I thought it's a problem of threads or object programming so I decide to simplify things to detect the source of the problem. I found the function that causes all problems. I put it in the main and execute it sequentially for many times with random parameters. It crashes between the 146000-156000 time. (To be sure I had re-execute the function with the parameters where it crash but it execute normally).

I had notice that putting the parameter object as global variable improves the number of time (it's 78000 when the 2 objects are parameters and about 150000 when they are global), other parameters are simple integer

I'm using many vectors as local variable of the function but I think this can't be the reason since they are destructed when the function finish the treatment.

What can be the reason of this crash?

for (int i=0;i<600000; i++){

    int _trx=-1020+((rand())%2039);
    int _try=-1020+((rand())%2039);
    double _or=(rand()%36000)/100;
    int _scale=rand()%2 -1 ;
    std::cout << i <<": trans x: " << _trx << " | trans y: " << _try << " | or: " << _or  << std::endl;

    double _score= Test(1,_trx,_try, _or,1,0,5,5,1024);
    cout <<" |score: " << _score << endl;

}
Gerhard
  • 6,850
  • 8
  • 51
  • 81
2K_tf
  • 17
  • 6
  • 3
    Hard to say with no code posted – Support Ukraine Nov 09 '17 at 06:58
  • Also it would be useful if you provided the output of the program just before it dies. Is it a segfault? Is it an abort? Is it an exception? – Botond Dénes Nov 09 '17 at 06:59
  • 1
    It is typical for multi-threading issues that errors occur sporadically (due to the non-determinism in multi-threading). In C++, you get less help from the compiler or standard libs regarding this. You can only "sharpen your eyes" or switch to a language with better MT support (e.g. Rust). Seriously: Please edit your question and send an [MCVE](http://stackoverflow.com/help/mcve). – Scheff's Cat Nov 09 '17 at 07:02
  • 1
    If crashes occur everytimes after round-about the same amount of iterations, this could be a hint for some "out-of"-issue (e.g. out of memory) which is unchecked. To check this, a look into the stack trace (core dump) may help. – Scheff's Cat Nov 09 '17 at 07:06
  • 1
    Use [valgrind](http://valgrind.org/) to spot the memory leaks. – Sami Hult Nov 09 '17 at 07:22
  • I have no error in the terminal at all ..sometimes it fineshed the function (I have many cout in the test to detect where it crash) and dosn t return to the cout and someother times It doesn t finsh it @Scheff – 2K_tf Nov 09 '17 at 09:58
  • I eliminate the cause of multi-threading by transforming the code in a simple function only to search the cause – 2K_tf Nov 09 '17 at 09:58
  • 1
    Compile with all warnings and debug info: `g++ -Wall -Wextra -g`with [GCC](http://gcc.gnu.org/). Read about [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) since you probably got some. **Use the debugger `gdb`** and next time you ask a question on SO provide some [MCVE] – Basile Starynkevitch Nov 09 '17 at 09:59
  • 1
    In the exposed source code, I cannot see anything suspicious except the call of `Test(1,_trx,_try, _or,1,0,5,5,1024);` about I don't know what it does. Thus, again: Please, provide an [MCVE](http://stackoverflow.com/help/mcve) so that anybody can reproduce your observed behavior (or just get a clue what might be wrong). You may also consider that (due to [Undefined Behavior](https://stackoverflow.com/a/4105123/1505939)), a program does not necessarily crash at the point where the error is but sometimes much later. – Scheff's Cat Nov 09 '17 at 11:39
  • if you're using C++, better avoid `rand` and use [`std::random_device` and `std::uniform_real_distribution`](https://stackoverflow.com/q/19665818/995714) instead – phuclv Nov 09 '17 at 17:14
  • The problem is in here -> `Test(1,_trx,_try, _or,1,0,5,5,1024);` – Gerhard Nov 14 '17 at 06:55

1 Answers1

3

Everyone knows that after about 50000 calls the C++ calling system becomes unreliable and you need to stop and start another prorgam.

NOT

C++ function calls do work.

Re-read an internalize this phrase. It's important for you to progress in programming.

The biggest problem you need to fix is in my opinion inside your brain, not in the code. If your first thought when you have a problem is thinking the other's people code (e.g. the compiler, the OS) or the hardware is the reason then you're not going to walk very far into programming.

Once you're ready to admit your code is the problem add more information to the question providing a source snippet, the exact error message, more context.

From the few symptoms you're providing my wild wild guess is a memory leak and your program dies for memory exhaustion, but no one really can tell for sure without more infos.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Memory exhaustion was my first idea. Regarding multi-threading, my 2nd idea was that muteces are also a limited resource. Thinking more, I surely would find more "wild guesses"... – Scheff's Cat Nov 09 '17 at 07:20
  • 2
    @Scheff: The biggest problem of the author needs to fix is in my opinion inside his/her brain, not in the code. If your **first** thought when you have a problem is thinking the other's people code is the source you're not going to walk very far into programming. – 6502 Nov 09 '17 at 07:24
  • I didn t say the pb isn t in my code ..I do many and many search and tests before asking about a limit of c++ function ..I have no error at all ..and at the most of tries the function terminates ( i had a cout step by step in the function )..I thought about the memories that why I m asking if really the local variables are destructed at the end of a function or it can be a pb of allocation var. .. I want just to be sure there is no limit of calling a function to avoid wasting more time in searching – 2K_tf Nov 09 '17 at 09:40
  • "Everyone knows that after about 50000 calls the C++ calling system becomes unreliable and you need to stop and start another prorgam." ?? I didn t understand this ..Is it true ?? if so this can be the cause since I m talking about 150000 call !! – 2K_tf Nov 09 '17 at 10:02
  • @2K_tf "Everyone knows that after about 50000 calls the C++ calling system becomes unreliable and you need to stop and start another prorgam." It's a joke. Consider the big **NOT** below. – Scheff's Cat Nov 09 '17 at 11:32
  • @scheff thx explainning this ..oki – 2K_tf Nov 09 '17 at 12:32