Passing string variable to CCCallFuncND

Hi everyone.

I got a question when using CCCallFuncND, how can I pass an std:string to it.
For example, I defined the function:

void myLayer::playSoundNamed(std::string soundFile)
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(soundFile.c_str());
}

Now, I want to execute that function by using CCCallFuncND… I saw the following way on an snippet:

CCCallFuncND *soundAction = CCCallFuncND::create(this, callfuncND_selector(myLayer::playSoundNamed), (void*)&soundFilepath);

However, that’s wrond, the value that comes into soundFile is ‘12sn’ or something like that.

Sorry guys, I’m new to C++ coming from the Objetive-C Cocos2d framework, so this might be a pretty obvious question.
Thanks a lot.

hi.

CCCallFuncND soundAction = CCCallFuncND::create(this, callfuncND_selector(myLayer::playSoundNamed), (void)&soundFilepath);
the third param of create is void*, and here, you have void, so, it's wrong, remove (void) or change to (void*);

typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*);
from here,we can see the callbackfunc needs two params: CCNode**, void**
so,

void myLayer::playSoundNamed(std::string soundFile) {
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(soundFile.c_str());
}

can change to:

 void myLayer::playSoundNamed(CCNode* node, void* soundFile) 
            or void myLayer::playSoundNamed(CCNode* node, std::string* soundFile) 
if used void*, we need add (std::string*) in callbackFunc.

last point.
std::string soundFilePath = “*****";
after the function execution, the memory of soundFilePath will be cleared, so it will be not accessible from the callbackfunc
you can try:
<pre>
std::string
soundFilePath = new std::string(”abcsadfads");
this~~>runAction, soundFilePath
)
);
void MineScene::playSoundNamed {
CCLogstr)~~>c_str());
delete str; //you will have to delete this, as this is a potential memory leak
}

Ok, so in the end I preferred to use CCString instead of std:string. However I got problem with the autorelease cycle.

void MyLayer::playSoundNamed(CCNode node, CCStringsoundFile);

if we call the function within a CCCallFunction and by doing
CCString *soundFile = CCString::create(“soundFile.wav”);

CCCallFuncND::create(this, callfuncND_selector(MyLayer::playSoundNamed),soundFile);

It will crash during the execution of the function because CCString was already relased. Now the solution is to retain it before sending it to the function
soundFile->retain();

And then within the function releasing it… however it’s not good, think is not reusable (because the caller MUST retain an object doing that).
Do you know a better way of doing this without usage of deletes or releases within the callback function?

i have an idea: we can use “soundFile” as a member variable of the class, then we can access it in other member function.

And then within the function releasing it… however it’s not good, think is not reusable (because the caller MUST retain an object doing that).
Do you know a better way of doing this without usage of deletes or releases within the callback function?