{"action":"created","issue":{"url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/27","labels_url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/27/comments","events_url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/27/events","html_url":"https://github.com/meh/rust-ffmpeg/issues/27","id":106502620,"number":27,"title":"Unsafe destructors","user":{"login":"lummax","id":6685542,"avatar_url":"https://avatars.githubusercontent.com/u/6685542?v=3","gravatar_id":"","url":"https://api.github.com/users/lummax","html_url":"https://github.com/lummax","followers_url":"https://api.github.com/users/lummax/followers","following_url":"https://api.github.com/users/lummax/following{/other_user}","gists_url":"https://api.github.com/users/lummax/gists{/gist_id}","starred_url":"https://api.github.com/users/lummax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lummax/subscriptions","organizations_url":"https://api.github.com/users/lummax/orgs","repos_url":"https://api.github.com/users/lummax/repos","events_url":"https://api.github.com/users/lummax/events{/privacy}","received_events_url":"https://api.github.com/users/lummax/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-09-15T07:55:34Z","updated_at":"2015-09-15T15:46:13Z","closed_at":null,"body":"Please consider the following snippet:\r\n\r\n```rust\r\nextern crate ffmpeg;\r\n\r\nuse std::env;\r\nuse std::path::Path;\r\n\r\nstruct Store {\r\n    input: ffmpeg::format::context::Input,\r\n    decoder: ffmpeg::codec::decoder::Audio,\r\n}\r\n\r\nfn main() {\r\n    ffmpeg::init().unwrap();\r\n\r\n    let ictx    = ffmpeg::format::input(&env::args().nth(1).expect(\"missing input\")).unwrap();\r\n    let decoder = ictx.streams().best(ffmpeg::media::Type::Audio).expect(\"could not find best audio stream\")\r\n                      .codec().decoder().audio().unwrap();\r\n\r\n    let store = Store {\r\n        input: ictx,\r\n        decoder: decoder,\r\n    };\r\n}\r\n```\r\n\r\nIf this gets compiled and run under `valgrind` several `Invalid read` and `Invalid writes` pop up:\r\n\r\n```\r\n==14420== Invalid read of size 8\r\n==14420==    at 0x5127F94: avcodec_close (utils.c:2880)\r\n==b14420==    by 0x11C2BC: codec::decoder::opened::Opened.Drop::drop::hd94913beb95d20d4Zdu (opened.rs:74)\r\n==14420==    by 0x111101: ffmpeg..codec..decoder..opened..Opened::drop.3455::h73bd8800b3ed73e0 (in /home/me/dev/rust-ffmpeg/target/debug/examples/test)\r\n==14420==    by 0x11109A: ffmpeg..codec..decoder..audio..Audio::drop.3452::h68a2d653d5c11a74 (in /home/me/dev/rust-ffmpeg/target/debug/examples/test)\r\n==14420==    by 0x11146B: Store::drop.3479::hb9aa8e79435c8851 (in /home/me/dev/rust-ffmpeg/target/debug/examples/test)\r\n```\r\n\r\nIf we change the order of the fields in the `Store` struct and leverage the Rust drop order the error is resolved:\r\n\r\n```rust\r\nstruct Store {\r\n    decoder: ffmpeg::codec::decoder::Audio,\r\n    input: ffmpeg::format::context::Input,\r\n}\r\n```\r\n\r\nThe problem might be, that `avcodec_free_context()` does not `NULL` the internal codec pointers and so a subsequent `avcodec_close()` operates on invalid data."},"comment":{"url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/comments/140437589","html_url":"https://github.com/meh/rust-ffmpeg/issues/27#issuecomment-140437589","issue_url":"https://api.github.com/repos/meh/rust-ffmpeg/issues/27","id":140437589,"user":{"login":"meh","id":40204,"avatar_url":"https://avatars.githubusercontent.com/u/40204?v=3","gravatar_id":"","url":"https://api.github.com/users/meh","html_url":"https://github.com/meh","followers_url":"https://api.github.com/users/meh/followers","following_url":"https://api.github.com/users/meh/following{/other_user}","gists_url":"https://api.github.com/users/meh/gists{/gist_id}","starred_url":"https://api.github.com/users/meh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/meh/subscriptions","organizations_url":"https://api.github.com/users/meh/orgs","repos_url":"https://api.github.com/users/meh/repos","events_url":"https://api.github.com/users/meh/events{/privacy}","received_events_url":"https://api.github.com/users/meh/received_events","type":"User","site_admin":false},"created_at":"2015-09-15T15:46:13Z","updated_at":"2015-09-15T15:46:13Z","body":"```\r\n==32237== HEAP SUMMARY:\r\n==32237==     in use at exit: 670,385 bytes in 38 blocks\r\n==32237==   total heap usage: 1,642 allocs, 1,604 frees, 6,284,830 bytes allocated\r\n==32237== \r\n==32237== 840 (96 direct, 744 indirect) bytes in 1 blocks are definitely lost in loss record 19 of 33\r\n==32237==    at 0x4C2AED6: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)\r\n==32237==    by 0x4C2AFF1: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)\r\n==32237==    by 0x4E55713: av_malloc (in /usr/lib/libavutil.so.54.31.100)\r\n==32237==    by 0x4E55951: av_mallocz (in /usr/lib/libavutil.so.54.31.100)\r\n==32237==    by 0x5658400: avcodec_open2 (in /usr/lib/libavcodec.so.56.60.100)\r\n==32237==    by 0x11A295: codec::decoder::Decoder::open_as::hb3b6bd33624a3e4cofu (mod.rs:44)\r\n==32237==    by 0x11A54B: codec::decoder::Decoder::audio::h198d9f48888689c6Hhu (mod.rs:85)\r\n==32237==    by 0x10D4F5: main::h42410289e5d85847naa (test.rs:15)\r\n==32237==    by 0x1220D4: rt::unwind::try::try_fn::h3491105338025906414 (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x11FDA8: __rust_try (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x121DBF: rt::lang_start::h91eee2133eb397d6ilx (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x1102E6: main (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237== \r\n==32237== 596,712 (7,712 direct, 589,000 indirect) bytes in 1 blocks are definitely lost in loss record 33 of 33\r\n==32237==    at 0x4C2AED6: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)\r\n==32237==    by 0x4C2AFF1: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)\r\n==32237==    by 0x4E55713: av_malloc (in /usr/lib/libavutil.so.54.31.100)\r\n==32237==    by 0x4E55951: av_mallocz (in /usr/lib/libavutil.so.54.31.100)\r\n==32237==    by 0x5658997: avcodec_open2 (in /usr/lib/libavcodec.so.56.60.100)\r\n==32237==    by 0x11A295: codec::decoder::Decoder::open_as::hb3b6bd33624a3e4cofu (mod.rs:44)\r\n==32237==    by 0x11A54B: codec::decoder::Decoder::audio::h198d9f48888689c6Hhu (mod.rs:85)\r\n==32237==    by 0x10D4F5: main::h42410289e5d85847naa (test.rs:15)\r\n==32237==    by 0x1220D4: rt::unwind::try::try_fn::h3491105338025906414 (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x11FDA8: __rust_try (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x121DBF: rt::lang_start::h91eee2133eb397d6ilx (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n==32237==    by 0x1102E6: main (in /home/meh/projects/rust/ffmpeg/target/debug/examples/test)\r\n```\r\n\r\nIf I remove the `Drop` for `Opened` we get a leak tho."}}