Bad Access returning NSObject

59 Views Asked by At

There is a fairly common bug I am getting where returning an NSObject created by Kotlin Multiplatform from a C++ class method causes an EXC_BAD_ACCESS (reported by Sentry Error Tracking). Why would this occur and what can be done to prevent it?

Here is the relevant portion of the class:

AgraQML::MountPoint::MountPoint(SMLMountPoint* point) {
    [point retain];
    m_mountPoint = point;
}

AgraQML::MountPoint::~MountPoint() {
    [m_mountPoint release];
}

SMLMountPoint* AgraQML::MountPoint::getMountPoint() const {
    return m_mountPoint;
}

And here is how the MountPoint and SMLMountPoint are instantiated:

SMLMountPoint *point = [[SMLMountPoint alloc] initWithRecord:mountPointRecord.toNSString()];
auto oldMountPoint = findChild<AgraQML::MountPoint*>();
AgraQML::MountPoint* mountPoint;
if (oldMountPoint == nullptr || oldMountPoint->path() != QString::fromNSString(point.path)) {
    // Make a new MountPoint
    mountPoint = new AgraQML::MountPoint(point);
    mountPoint->setParent(this);
    if (oldMountPoint != nullptr) // Remove the outdated MountPoint
        oldMountPoint->deleteLater();
} else {
    // Use the old MountPoint
    mountPoint = oldMountPoint;
}

Here is the exception that occurred:

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: SEGV_NOOP at 0x0000000000000010
Crashed Thread: 0

Application Specific Information:
__cxa_guard_release > _setInvalidatesViewsOnAppearanceChange: > application:didFinishLaunchingWithOptions: >
KERN_INVALID_ADDRESS at 0x10.

Thread 0 Crashed:
0   AgraGPS                         0x2009acb74         AgraQML::MountPoint::getMountPoint (MountPoint.mm:16)
1   AgraGPS                         0x20099877c         AgraQML::NTRIP::restoreGetData (NTRIP.mm:61)
0

There are 0 best solutions below