How to pass parameters to my task in C++?

I want to call a function every frame so I used a task added to taskMgr.
But two variables defined in my main function is necessary in my task function, and I don’t know how to pass my parameters to it.
I’ve read some similar problem in forums but they are for python, using the “extraargs”,
but I find that C++ don’t have such parameter? is it? So I need help that how to pass my parameters defined in the main function to my task function.

Beside, I try to use global variable first to solve the problem but it went wrong, it’s another problem confused me.
If I do this PT(BulletWorld) physics_world; in my main function, it is ok;
but when I move it to out as a global variable, it triggered an exception. It seems I can’t use the PT(BulletWorld) physics_world as a global variable, although my global nodepath variables works well meanwhile.

oh, it can be done by using the third parameter in GenericAsyncTask()

However, even I pass my PT(BulletWorld) (and PT(BulletRigidBodyNode)) into my task function, same strange exception is triggered. It looks like this

Specifically, the panda window occurred but vanished quickly,
the printed “0” proved the sentence in my main function (before the task function) run, so I think the problem is due to the PT(BulletWorld) in my task function.

Are you keeping a reference around to the BulletRigidBodyNode and BulletWorld? The void* argument does not keep the reference count incremented, so you will need to make sure you have at least one PT() reference to these objects in some other place.

On a sidenote, you can also create your own task by creating a class that inherits from AsyncTask and overrides the virtual DoneStatus do_task(); method. That will let you store any references to whatever objects you want as members of the class.

Oh, I remembered that actually the problem occurred early but I didn’t pay attention to it.
I try to describe it simply:
when I do the following sentence both in my main function:
PT(BulletWorld) physics_world;
physics_world = new BulletWorld();
it works well.
But when I move PT(BulletWorld) physics_world; out of my main function, (actually at the first of my whole code) to state it as a global variable, my program crashed with the exception.
As I just make the one change, so I’m sure the reason is due to the statement position of the PT(BulletWorld) variable, but I don’t now why and so can’t solve it so far, since other types of variables (e.g., NodePath) can work well in the same way.

Could you show more complete code, as well as the call stack at the time of the crash?

It’s a little awkward…
I don’t know what I did, I think I didn’t change anything meaningful and when I receive your response, I cut my code to a short and simple version, but I found it unexpectedly worked… then I look back to my origin code, and it also worked now…
so maybe I won’t know why the exception occurred because it disappear, maybe the main problem is that I’m not good in C++…
However, thanks again Mr. rdb, you solve all of my problem these days, due to your generous help, I really enjoy panda3d now.