04-20-2012 11:35 AM
i just happened to need do a simple program but i got this problem,
basically what i need to do is switching some Sprites, from a list of them.
in the init function i have this,
//list<CCSprite*> images. in the header which add the images.
//list<CCSprite*>::iterator imagetator; // obvious
for(int n = 0;n<30;n++,IMAGE_AMOUNT++)
CCSprite *sprite = new CCSprite;
CCSprite *sprite2 = CCSprite::spriteWithFile("my.png");
speed = 20;
decrease = 0.03;
imagetator = images.begin();
and this is the function that is supppose to do the magic, and is is supose to go forward the list and
putting each sprite in the front. But when is executes for a little time, it goes crazy and start leaking,
slow down the device. I haven't realize what's up with this behabior
if(speed > 0)
if(imagetator == images.end()) imagetator = images.begin();
previous = *imagetator;
if(imagetator != images.end())
else imagetator = images.begin();
else if(speed <= 0)
04-20-2012 12:08 PM
I don't know anything about the scheduler and it's threadbehaviour. Maybe you get into trouble when change is executed twice the same time. Try to increase the time between calls to make shure change is ready before the next call. Or use a mutex to avoid double execution.
At my opinion you should also avoid pointeroperations. The can result in really silly bugs. Better try to use  to acess the elements even it is slower.
But maybe I'm completely wrong.
04-20-2012 01:33 PM
Actually you are right, i change the time the schedule() call my function, it does run nicelly. Meaning that i have to deal with locks now, by the way i have no idea how to implement them there. Could you advise me something for that, links or another way instead if the schedule() thing?
04-20-2012 04:48 PM
I'm using Qt. There are classes like QMutex and QMutexLocker to implement it really easy.
You can search the inet for c and mutex or semaphore.
But if you block the threads with a mutex, you will have many threads at the end if execution is to slow. So just block if no other thread is executing the critical code.
I think it should look like this:
Bool locked = false;
If (locked == true ) return;
Loked = true
... Do your work
.. Unlock mutex
Locked = false