Skip to content

How we pass a function into objc2? #758

@b01o

Description

@b01o

I am currently using objc2_core_media to construct a CMSampleBuffer. However, I cannot figure out how to pass a "c-unwind" ABI style function into objc2 function.

For example, the following code compiles but it segfaults when the newly constructed CMBlockBuffer goes out of scope.

use objc2::rc::Retained;
use objc2_core_foundation::*;
use objc2_core_media::*;

use std::{
    ffi::c_void,
    ptr::{NonNull, null_mut},
};

fn main() {
    unsafe extern "C-unwind" fn free_block(
        ref_con: *mut c_void,
        block: NonNull<c_void>,
        block_size: usize,
    ) {
        println!("free_block called");
    }

    let data = vec![65u8; 1024];
    let data = data.leak();
    let custom_block_source = CMBlockBufferCustomBlockSource {
        version: 0,
        AllocateBlock: None,
        FreeBlock: Some(free_block),
        refCon: null_mut(),
    };
    let block_buffer: *mut CMBlockBuffer = null_mut();
    let status = unsafe {
        CMBlockBuffer::create_with_memory_block(
            None,
            data.as_ptr() as *mut std::ffi::c_void,
            data.len(),
            kCFAllocatorNull,
            &custom_block_source,
            0,
            data.len(),
            0,
            NonNull::new(&block_buffer as *const _ as *mut _).unwrap(),
        )
    };
    assert_eq!(status, 0);

    let block_buffer = unsafe { Retained::from_raw(block_buffer).unwrap() };
    assert_eq!(block_buffer.retain_count(), 1);
}

Note that the free_block does not being called as well. Am I missing something on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frameworkAffects the framework crates and the translator for themI-unsoundA soundness hole, or affecting soundnessbugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions