How to prevent a root object from being deleted in C++/Objective-C

I have the following code in my C++/Objective-C app:

#import <YandexMobileAds/YandexMobileAds-Swift.h>

@class AdHook;

namespace MyApp
{
    class AdHolder
    {
    public:

        AdHolder(IosInterstitialAd* cpp_ad);

        AdHook* m_hook;
    };
}

using namespace MyApp;

@interface AdHook : NSObject <YMAInterstitialAdLoaderDelegate, YMAInterstitialAdDelegate>
{
    @public IosInterstitialAd* m_cppAd;

    @public YMAInterstitialAdLoader* m_loader;
    @public YMAInterstitialAd* m_interstitialAd;
}
@end

@implementation AdHook 

- (id)initFromCpp:(IosInterstitialAd *)cpp_ad
{
    if (self = [super init])
    {
        m_cppAd = cpp_ad;
        m_interstitialAd = nil;

        m_loader = [[YMAInterstitialAdLoader alloc]init];
        m_loader.delegate = self;
    }
    return self;
}

AdHolder::AdHolder(IosInterstitialAd* cpp_ad)
{
    [YMAMobileAds enableLogging];

    m_hook = [[AdHook alloc]initFromCpp:cpp_ad];
}

void SomeOtherClass::load()
{
    NSLog(@"IosInterstitialAd::load()");

    YMAAdRequestConfiguration* configuration = [[YMAAdRequestConfiguration alloc]initWithAdUnitID:@"demo-interstitial-yandex"];

    [m_holder->m_hook->m_loader loadAdWithRequestConfiguration:configuration];
}

I create an instance of AdHolder class in C++ code and AdHolder‘s constructor creates an instance of Objective-C class AdHook that in its turn crates its internal Objective-C objects.

But I do not understand well enough what is the lifetime of m_hook? And I suspect that it is deleted at some point, because when I try to access m_hook->m_interstitialAd from YMAInterstitialAdLoaderDelegate callback I get a wrong value and the app crashes.

Is m_hook a strong reference? If it is not how to make it strong?

EDIT1

Tried to randomly add retain call to various objects in my app and made the app work by adding retain call to AdHook::m_interstitialAd:

- (void)interstitialAdLoader:(YMAInterstitialAdLoader * _Nonnull)adLoader didLoad:(YMAInterstitialAd * _Nonnull)interstitialAd
{
    NSLog(@"IosInterstitialAd::YMAInterstitialAdLoader::didLoad, %@", interstitialAd);

    m_interstitialAd = [interstitialAd retain];
    m_interstitialAd.delegate = self;
}

at least my app stopped crashing when I access m_interstitialAd.

So the question probably is if AdHook::m_interstitialAd a strong reference?

7

The issue likely stems from m_hook not being a strong reference in your C++ class. In Objective-C/C++ interop, plain pointers don’t imply ownership. To create a strong reference, modify your AdHolder class to use a pointer to a pointer (AdHook** m_hook;). In the constructor, allocate it with m_hook = new AdHook*; *m_hook = [[AdHook alloc] initFromCpp:cpp_ad];. Add a destructor to clean up: if (m_hook) { [*m_hook release]; delete m_hook; }. When accessing, use (*holder()->m_hook)->m_loader. This ensures the AdHook object isn’t deallocated unexpectedly. Also, verify that the AdHolder instance itself remains valid when you’re accessing m_hook, as its premature destruction could lead to crashes.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật