I have an Objective-C object named "LocationGetter", and a companion C++ object named "CppLocationGetter". The two objects are properties of each other, and they are supposed to communicate with each other. Unfortunately, when the C++ object sends "startLocationUpdates" to the Objective-C object, an EXC_BAD_ACCESS code 2 error results. Edited to add: The debugger says "CppLocationGetter"'s getter property is of type "observer *", although I did not define that type. Also, the memory address appears to be identical to that of my C++ object's "observer" property.
LocationGetter.m:
LocationGetter.h:
LocationGetter.m:
Code:
#import "LocationGetter.h"
#ifdef __cplusplus
usingnamespaceCppLocation;
CppLocationGetter::CppLocationGetter()
{
LocationGetter *getter = [[LocationGetter alloc] initWithCppObject:this];
this->getter = (__bridge void *)getter;
}
void CppLocationGetter::startLocationUpdates()
{
[(__bridgeLocationGetter *)getter startLocationUpdates]; // EXC_BAD_ACCESS
}
#endif
#ifdef __OBJC__
@implementation LocationGetter
{
CppLocation::CppLocationGetter *CppObj;
}
-(instancetype)initWithCppObject:(CppLocationGetter *)getter
{
self = [self init];
if (self) {
CppObj = getter;
}
returnself;
}
-(void)startLocationUpdates
{
CppObj->CppLocationGetter::observer->locationUpdated(10, 01, 10);
}
@end
#endif
Code:
#ifdef __cplusplus
namespace CppLocation
{
struct CppLocationObserver
{
virtual void locationUpdated(float latitude, float longitude, float accuracy) = 0;
};
class CppLocationGetter {
void *getter;
public:
CppLocationGetter();
CppLocationObserver *observer;
void startLocationUpdates();
};
}
#endif
#ifdef __OBJC__
#include <Foundation/Foundation.h>
@interface LocationGetter : NSObject
-(instancetype)initWithCppObject:(CppLocation::CppLocationGetter *)getter;
-(void)startLocationUpdates;
@end
#endif