c++ - 从 Objective C 调用 C++ 回调后如何释放桥接对象

标签 c++ objective-c callback cocos2d-x

我正在使用 cocos2d-x 在 C++ 中创建一个应用程序。对于一些集成工作,我需要调用 Objective C 代码,通过从 Objective C 调用 C++ 回调方法实现异步响应。

TLDR;每当回调完成时,我应该如何将 C++ 释放到 Objective C 桥?我应该将桥实现为静态成员变量吗?

对于从 C++ 到 Objective C 的基本部分,我遵循了 Jesús Bosch's lead ,对于从 Objective C 调用 C++ 回调,我遵循了他的 colleagues post .

基本上,我已经完成了以下设置。

首先,我们有MyApp.h

class MyApp
{
public:
    static void buttonChosen(int index);
    void callObjC();
    // ...
}

MyApp.cpp,负责启动 Objective C 代码。

void MyApp::buttonChosen(int index)
{
    log("Button choosen: %d", index);
}

void MyApp::callObjC()
{
    iOSBridge::iOSHelper* bridge = new iOSBridge::iOSHelper();
    bridge->ShowAlert(buttonChosen);        
    // delete bridge;
}

然后,我有了桥,iOSBridge.h

namespace iOSBridge{
    class iOSHelper {    
    public:
        void(*callback)(int index);

        iOSHelper() { }
        void ShowAlert(void(*callback)(int));    
    };    
}

及其实现iOSHelper.cpp

#include "iOSHelper.h"
#import "IsolatedAlert.h"

namespace iOSBridge{    
    void iOSHelper::ShowAlert(void(*callback)(int index))
    {
        this->callback = callback;
    IsolatedAlert* instance = [IsolatedAlert new];
    [instance showAlert: this];
    }
}

最后是IsolatedAlert.h

#import <UIKit/UIKit.h>
#import "iOSHelper.h"

typedef struct iOSBridge::iOSHelper iOsType;

@interface IsolatedAlert : UIViewController
-(void)showAlert:(iOsType*)callback;
@end

及其实现IsolatedAlert.mm

#import "IsolatedAlert.h"

iOsType* staticPointer;

@implementation IsolatedAlert
-(void)showAlert:(iOsType*)callback
{
    staticPointer = callback;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title?"
                                                    message:@"Message?"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
}

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    staticPointer->callback(buttonIndex);
}
@end

现在,我的问题是:每当调用 buttonChosen() 时,我应该如何释放 iOSBridge::iOSHelper* bridge?是否可以将其作为静态成员实现,只要我保证在任何时候都不会实例化多个 MyApp 实例?谢谢!

最佳答案

通常C++ way of releasing an instancenew创建就是使用delete关键字:

void MyApp::buttonChosen(int index)
{
    log("Button choosen: %d", index);

    delete bridge;
    bridge = NULL;
}

最好在删除后将指针NULL,以确保在delete 后使用对象时应用程序以一致的方式运行(崩溃)。如果您不这样做,您的应用程序可能会工作一段时间、产生不正确的结果、表现出奇怪的行为或随机崩溃,或者出现上述所有情况。

啊,当然你应该让桥指针成为你的 App 类的成员变量。

#include "iOSHelper.h"

class MyApp
{
public:
    static void buttonChosen(int index);
    void callObjC();
    // ...

protected:
    iOSBridge::iOSHelper* bridge;
}

您还应该考虑将现有方法设为 protectedprivate,因为 buttonChosen 可能不是您希望能够从 MyApp 实异常(exception)部调用的方法。

关于c++ - 从 Objective C 调用 C++ 回调后如何释放桥接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037970/

相关文章:

c++ - string.find 函数返回奇数

c++ - 使用模板和继承时出现问题,错误x86_64体系结构的 undefined symbol :

javascript - 如何使用 javascript 回调函数检测文本区域中的 url?

c++ - Windows Mobile WAP 唤醒

C++ 错误 C2819 : type 'List' does not have an overloaded member 'operator ->'

iphone - 根据应用设置而不是设备设置,在 UIDatepicker 中以 12 小时和 24 小时格式显示时间

objective-c - 当我将字符串粘贴到Xcode中时,总是会出现错误,必须重新输入

ios - 如何关闭多个文本字段的所有键盘?

javascript - 当作为参数传递时,对象方法回调会丢失其在事件处理程序中的绑定(bind),但在硬编码时不会

java - Android 原生守护进程通过 JNI 调用 Java 方法