gpt4 book ai didi

linux - 开关中断时 D 语言中的随机段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:37 25 4
gpt4 key购买 nike

我正在调试一个用 D 编写的相当简单的程序,它似乎有随机机会接收 SEGV 信号。经过进一步检查,我发现使用不同的编译器和构建模式会产生不同的结果。

我的测试结果:

  • DMD 调试 = 99% 的时间都有效
  • DMD 发布 = 50/50
  • 最不发达国家调试 = 50/50
  • 最不发达国家发布 = 50/50

因为默认编译器 (DMD) 的二进制文件仅在我无法真正调试它时崩溃,并且由于缺少调试符号, Release模式也无济于事。在 Debug模式下使用 LDC 构建二进制文件让我使用 gdb 和 valgrind 对其进行测试,以总结我收集到的内容。

来自valgrind的相关信息,

Invalid read of size 4 @ ctor in file video.d line 46

Access not within mapped region at address 0x0 @ ctor in file video.d line

Gdb 没有给我更多的见解,3 个堆栈帧,其中只有第 0 个感兴趣,第 0 帧的回溯显示文件 video.d 第 46 行是一个 break 语句,那现在怎么办?

这是产生段错误的代码片段

module video;

import ffmpeg.libavformat.avformat;
import ffmpeg.libavcodec.avcodec;
import ffmpeg.libavutil.avutil;

class Foo
{
private
{
AVFormatContext* _format_ctx;
AVStream* _stream_video;
AVStream* _stream_audio;
}

...

public this(const(string) path)
{
import std.string : toStringz;

_format_ctx = null;
enforce(avformat_open_input(&_format_ctx, path.toStringz, null, null) == 0);
scope (failure) avformat_close_input(&_format_ctx);

enforce(avformat_find_stream_info(_format_ctx, null) == 0);
debug av_dump_format(_format_ctx, 0, path.toStringz, 0);

foreach (i; 0 .. _format_ctx.nb_streams)
{
AVStream* stream = _format_ctx.streams[i];

if (stream == null)
continue;

enforce (stream.codecpar != null);

switch (stream.codecpar.codec_type)
{
case AVMediaType.AVMEDIA_TYPE_VIDEO:
_stream_video = stream;
break;
case AVMediaType.AVMEDIA_TYPE_AUDIO:
_stream_audio = stream;
break;
default:
stream.discard = AVDiscard.AVDISCARD_ALL;
break; // Magic line 46
}
}
}
}

// Might contain spelling errors, had to write it by hand.

那么有没有人知道是什么导致了这种行为,或者更准确地说是如何解决它?

最佳答案

尝试检查有效性_stream_audio

default:
enforce( _stream_audio, new Exception( "_stream_audio is null" ))
.discard = AVDiscard.AVDISCARD_ALL;
break; // Magic line 46

关于linux - 开关中断时 D 语言中的随机段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com