Hi all

I'm stuck with this problem for a couple of weeks now, and my normal human brain can't tell me what's going wrong.

I've created my own class, inheriting from QThread. Nothing weird in calling it:

MyThread *thread = new MyThread();
thread->start();
while(thread->isRunning) ;
delete thread

;

I know I don't need a pointer in the code above, but the problem remains even without it.

The only code implemented in the thread class is run()

void MyThread::run()
{
  cols = 8000;
  rows = 9000;

  double progress = 0.0;
  double add = 100.0/(cols*rows);

  for(int col = 0; col < cols; col++)
  {
    for(int row = 0; row < rows; row++)
    {
      progress += add;
    }
  }
}

So basically what the code does, it increases the progress (between 0 and 100). The last += add; will make progress 100;

There are absolutely no pointers in the thread.

Now the weird thing: After the execution of the thread, my program shows a memory usage between 10 and 40mb (after the thread finished, my program is basically in idle).

When I remove progress += add; from the code, the memory usage is only a few bytes (as it should be, since all the code inside run() is now out of scope). When I replace progress += add; with something like: progress = 1; then I also don't have a memory loss.

So the problem is somewhere in adding to a double value (memory loss with int instead of double is between 5mb-20mb).

Does anyone have a clue why this is happening, since I don't have any clue why variables that are out of scope can still take up memory?

Recommended Answers

All 2 Replies

Interesting.. Can you post full code? With main()? so I can run it in my CodeBlocks..

This is very weird. The problem must be a sort of stack wind-up of the for-loop (at low-level a for-loop is turned into a "label, set of instructions, and goto the start label", so if "set of instructions" increases the size of the stack you get a stack wind-up). But this is a bug, not an expected behaviour.

Try to increase the level of optimization, maybe the compiler can get rid of a useless temporary and thus, solving the problem at the same time. For instance, if I compile your function with highest level of optimization with GCC, it turns out that it eliminates the entire run() function because the compiler is clever enough to see that this function has no effect (it only modifies a local variable and returns nothing).

What compiler are you using? Have you tried alternative compilers? Make sure the one you have is recent and appropriate for your system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.