发生异常。使用 C 和 SDL EXC_BAD_ACCESS(code=1,address=0x0)

Exception has occurred. EXC_BAD_ACCESS (code=1, address=0x0) using C and SDL

提问人:Flipmode34 提问时间:11/3/2023 最后编辑:genpfaultFlipmode34 更新时间:11/3/2023 访问量:56

问:

我在尝试使用功能将SDL_TEXTURE*分配给另一个SDL_TEXTURE*时出现EXP_BAD_ACCESS错误。

我正在使用 VSCode 在 macOS Sonoma 14.1 上使用 clang 进行编译。

我尝试使用 malloc 将内存分配给 fps_texture->texture,但这导致了同样的错误。

在 main.c 中

GAME_TEXTURE* fps_texture = NULL;
fps_texture->texture = text_load(app.renderer, fps_font, "TEST"); // Getting error here
if (!fps_texture->texture)
{
   printf("Unable to load font into texture!\n");
}

text_load定义

SDL_Texture* text_load(SDL_Renderer* renderer, TTF_Font* font, char* text)
{
  SDL_Texture* texture = NULL;

  //Render text surface
  SDL_Surface* text_surface = TTF_RenderText_Solid(font, text, TEXT_COLOR);
  if (text_surface == NULL)
  {
    printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
  }
  else
  {
    //Create texture from surface pixels
    texture = SDL_CreateTextureFromSurface(renderer, text_surface);
    if (texture == NULL)
    {
      printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
    }
    else
    {
      //Get image dimensions
      // text_texture->width = text_surface->w;
      // text_texture->height = text_surface->h;
    }

    //Get rid of old surface
    SDL_FreeSurface(text_surface);
  }
  
  return texture;
}

结构体定义

typedef struct GAME_TEXTURE
{
  SDL_Texture* texture;
  uint32_t width;
  uint32_t height;
} GAME_TEXTURE;

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "main.c",
        "-I",
        "/opt/homebrew/Cellar/sdl2/2.26.5/include/SDL2",
        "-I",
        "/opt/homebrew/Cellar/sdl2_image/2.6.3_1/include/SDL2",
        "-I",
        "/opt/homebrew/Cellar/sdl2_ttf/2.20.2/include/SDL2",
        "-L",
        "/opt/homebrew/Cellar/sdl2/2.26.5/lib",
        "-L",
        "/opt/homebrew/Cellar/sdl2_image/2.6.3_1/lib",
        "-L",
        "/opt/homebrew/Cellar/sdl2_ttf/2.20.2/lib/",
        "-l",
        "SDl2",
        "-l",
        "SDL2_image",
        "-l",
        "SDL2_ttf",
        "-o",
        "main"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "Task generated by Debugger."
    },
    {
      "type": "cppbuild",
      "label": "C/C++: clang build active file",
      "command": "/usr/bin/clang",
      "args": [
        "-fcolor-diagnostics",
        "-fansi-escape-codes",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}

为什么无法访问内存?

c macOS 可视化工作室代码 SDL

评论

4赞 Some programmer dude 11/3/2023
GAME_TEXTURE* fps_texture = NULL;紧随其后的是 ?指针变量不指向任何位置,它是 .不能取消引用该指针变量。当你这样做时,你将有未定义的行为fps_texture->texture = ...fps_textureNULL
0赞 DerPhysiker 11/3/2023
正如用户 Some programmer dude 在上面所写的那样,在执行任何其他操作之前,您可能需要先初始化 use。不写这个作为答案,因为我现在无法在我的机器上测试它。GAME_TEXTURE* fps_texturemalloc
0赞 Flipmode34 11/3/2023
我现在通过分配 GAME_TEXTURE* 本身而不是其中的指针来让它工作:GAME_TEXTURE* fps_texture = (GAME_TEXTURE*)malloc(sizeof(GAME_TEXTURE));顺便说一句,这也有效:GAME_TEXTURE* fps_texture = 新GAME_TEXTURE;谢谢!
0赞 Ted Lyngmo 11/3/2023
@Flipmode34 既然你说“顺便说一句,这也有效:GAME_TEXTURE* fps_texture = new GAME_TEXTURE;” ,你实际上并不是在用 C 语言编程。您正在使用 C++ 或其他语言进行编程,您可以使用这些语言来创建对象。C不是其中之一。哦,我明白了,而且 - 所以绝对是 C++。new/usr/bin/clang++-std=c++17
0赞 Ted Lyngmo 11/3/2023
您不必使用 C++ 进行类。只要成功typedefstruct GAME_TEXTURE { ... };

答: 暂无答案