c - Pebble 菜单层崩溃

标签 c pebble-watch pebble-sdk

我一直在为原始的 pebble 设计一个个人新闻提要应用程序,它在菜单层中显示新闻内容。我有两个 C 文件,其中一个包含一个显示加载图像的窗口,并处理手机上的 js 和我的 watch 应用程序之间的数据传输。数据被保存到一些外部变量中,这些变量在文件之间共享,但与我下面显示的示例无关(我相信所有这些都工作正常)。

当满足某些条件时,我在函数内的主 .c 文件中调用 news_list_window_init()。现在我只是想显示一个带有可滚动菜单的窗口来测试我的菜单看起来是否可以接受。我遇到的问题是当我尝试滚动时我的应用程序崩溃了。窗口出现,我所有的菜单项都在那里但是当我滚动时应用程序存在:

ault_handling.c:78> App fault! {f5ec8c0d-9f21-471a-9a5c-c83320f7477d} PC: 0x800fd5b LR: ???
Program Counter (PC)    : 0x800fd5b  ???
Link Register (LR)      : ???        ???

我在下面独立测试了我的 .c 文件,我在其中创建了一个只有这段代码的新项目,注释掉了不相关的 news_list.hexterns.h头文件,并在文件底部的main函数中注释。这工作正常,我的菜单滚动并且没有崩溃,一切看起来都很好。

我看不出问题出在我的主文件中,因为我在主文件中调用的唯一函数是 news_list_window_init(),而且菜单确实显示正确。我什至可以使用后退按钮正确关闭应用程序。然而,尝试滚动会使应用程序崩溃。对于可能导致此错误的原因,我有点不知所措。有人有什么建议吗?谢谢!

这是相关的 .c 文件:

// news_list.c
#include "pebble.h"
#include "news_list.h"
#include "externs.h"

#define NUM_MENU_SECTIONS 1

static Window *news_list_window;
static MenuLayer *menu_layer;

static uint16_t menu_get_num_sections_callback(MenuLayer *menu_layer, void *data) {
  return NUM_MENU_SECTIONS;
}

static uint16_t menu_get_num_rows_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  return str_count; // Variable story count
}

static int16_t menu_get_header_height_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  return MENU_CELL_BASIC_HEADER_HEIGHT;
}

static void menu_draw_header_callback(GContext* ctx, const Layer *cell_layer, uint16_t section_index, void *data) {
  menu_cell_basic_header_draw(ctx, cell_layer, "Header");
}

static void menu_draw_row_callback(GContext* ctx, const Layer *cell_layer, MenuIndex *cell_index, void *data) {
  menu_cell_basic_draw(ctx, cell_layer, "Menu Item", NULL, NULL);
}

static void menu_select_callback(MenuLayer *menu_layer, MenuIndex *cell_index, void *data) {
  // Currently Empty
}

int16_t menu_get_cell_height_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *callback_context)
{
    return 40;
}

static void news_list_window_load(Window *window) {
  // Now we prepare to initialize the menu layer
  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_frame(window_layer);

  // Create the menu layer
  menu_layer = menu_layer_create(bounds);
  menu_layer_set_callbacks(menu_layer, NULL, (MenuLayerCallbacks){
    .get_num_sections = menu_get_num_sections_callback,
    .get_num_rows = menu_get_num_rows_callback,
    .get_header_height = menu_get_header_height_callback,
    .draw_header = menu_draw_header_callback,
    .draw_row = menu_draw_row_callback,
    .select_click = menu_select_callback,
    .get_cell_height = menu_get_cell_height_callback,
  });

  // Bind the menu layer's click config provider to the window for interactivity
  menu_layer_set_click_config_onto_window(menu_layer, window);

  layer_add_child(window_layer, menu_layer_get_layer(menu_layer));
}

static void news_list_window_unload(Window *window) {
  // Destroy the menu layer
  menu_layer_destroy(menu_layer);
}

void news_list_window_init() {
  news_list_window = window_create();
  window_set_window_handlers(news_list_window, (WindowHandlers) {
    .load = news_list_window_load,
    .unload = news_list_window_unload,
  });
  window_stack_push(news_list_window, true);
}

void news_list_window_deinit() {
  window_destroy(news_list_window);
}

// int main(void) {
//   news_list_window_init();
//   app_event_loop();
//   news_list_window_deinit();
// }

这是相关的 .h 文件:

// news_list.h
#ifndef NEWS_LIST_H
#define NEWS_LIST_H

// Public Function list
void news_list_window_init(void);
void news_list_window_deinit(void);

#endif

最佳答案

当我的内存非常低时,或者如果我以某种方式破坏内存,我就遇到过这种情况。

我建议检查所有返回值是否为 NULL,如果它们是(menu_layer_create、malloc 等)则进行日志记录和保释。

另外,试试 logging多少钱free memory你有 - 如果你正在使用原始的 Pebble,并为 communication 分配大内存缓冲区,您很快就会耗尽内存。

最后,遍历所有分配和使用内存的代码,以绝对确保您没有意外写入数组末尾,或将分配在堆栈上的值传递给 pebble 调用。我喜欢用 calloc而不是 malloc确保我认为我在 malloc 的结构中没有值(value),而我没有。

我以前来过这里 - 这并不容易,但可能与您的 UI 代码完全无关 - 崩溃只是一种症状。如果一切都失败了,可以将完整的代码发布到 github 中,以便我们可以查看完整的应用程序...

达米安

关于c - Pebble 菜单层崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36123781/

相关文章:

android - Google Tango Leibniz API 中的彩色图像

javascript - 菜单项可以单独着色吗?

c - 仅使用 C 宏反转 ASCII 字符串

compiler-errors - Pebble教程: “pebble build” and “pebble install” throwing errors on Arch machine

c - 我应该采取什么预防措施来制作不调用未定义行为的内存池?

c - 从文件中读取 C 中的不同类型

android - 有没有可以扫描BLE信标的可穿戴设备?

c - 如何在 C 中使用 Pebble SDK 将 int 转换为字符串

c - Pebble SDK - 带字符串的结构数组(可能还有指针)

c - 异步编程