rust-av / ffms2-rs

ffms2 bindings
MIT License
10 stars 5 forks source link

ResampleOptions drop frees invalid pointer #7

Closed askoufis closed 4 years ago

askoufis commented 4 years ago

Hi. I'm trying to work with SetOutputFormatA, but I can't even get a basic program to work because when ResampleOptions goes out of scope I get a free(): invalid pointer error. Here is an example program:

use ffms2::audio::*;
use ffms2::index::*;
use ffms2::track::*;
use ffms2::*;
use std::path::PathBuf;

fn main() {
    FFMS2::Init();

    let file_path = PathBuf::from("audio.mp3");
    let indexer = Indexer::new(&file_path).expect("Failed to create indexer");
    indexer.TrackTypeIndexSettings(TrackType::TYPE_AUDIO, 1);

    let index = indexer
        .DoIndexing2(IndexErrorHandling::IEH_IGNORE)
        .expect("Failed to index");

    let track_number = index
        .FirstIndexedTrackOfType(TrackType::TYPE_AUDIO)
        .expect("Could not find audio track");
    let audio_source = AudioSource::new(&file_path, track_number, &index, -1)
        .expect("Failed to create audio source");

    let resample_options = audio_source.CreateResampleOptions();

    audio_source
        .SetOutputFormatA(&resample_options)
        .expect("Could not set audio output format");
}

Output:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/scratch`
free(): invalid pointer
zsh: abort (core dumped)  cargo run
askoufis commented 4 years ago

I was going to sleep on it but figured it out. FFMS_AudioSource::CreateResampleOptions gives you a std::unique_ptr, so you can't just pass a normal mutable raw pointer to the destructor, you need to use Box::into_raw.

I'll make a PR for this.