gpt4 book ai didi

javascript - 将数据存储到系统时 Electron 应用程序崩溃(应用程序退出代码 3221225477)

转载 作者:行者123 更新时间:2023-12-05 05:40:32 25 4
gpt4 key购买 nike

我正在开发一个 Electron 应用程序,我正在使用以下类实现来保存和检索用户磁盘中的数据。有时这会正常运行,但有时程序会崩溃并输出此错误。

app exited with code 3221225477

我不太确定是什么导致了这个问题。我知道这个错误代码意味着发生了访问冲突,但我不确定为什么会这样。可能是类的实现。我只是从这里获取实现 https://medium.com/cameron-nokes/how-to-store-user-data-in-electron-3ba6bf66bc1e

也有这种情况随机发生的情况,所以它可能不是 Store 实现。

const electron = require('electron');
const path = require('path');
const fs = require('fs');


class Store {
constructor(opts) {
// Renderer process has to get `app` module via `remote`, whereas the main process can get it directly
// app.getPath('userData') will return a string of the user's app data directory path.
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
// We'll use the `configName` property to set the file name and path.join to bring it all together as a string
this.path = path.join(userDataPath, opts.configName + '.json');

this.data = parseDataFile(this.path, opts.defaults);
}

// This will just return the property on the `data` object
get(key) {
const val = this.data[key];
console.log('Get', key, val);
return val;
}

// ...and this will set it
set(key, val) {
console.log('Set', key, val)
this.data[key] = val;
// Wait, I thought using the node.js' synchronous APIs was bad form?
// We're not writing a server so there's not nearly the same IO demand on the process
// Also if we used an async API and our app was quit before the asynchronous write had a chance to complete,
// we might lose that data. Note that in a real app, we would try/catch this.
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}

function parseDataFile(filePath, defaults) {
// We'll try/catch it in case the file doesn't exist yet, which will be the case on the first application run.
// `fs.readFileSync` will return a JSON string which we then parse into a Javascript object
try {
return JSON.parse(fs.readFileSync(filePath));
} catch(error) {
// if there was some kind of error, return the passed in defaults instead.
return defaults;
}
}

// expose the class
module.exports = Store;

最佳答案

我似乎找到了这个问题的原因。以下帖子包含一条评论,上面写着

Keep a global reference of the window object, if you don't, the windowwill be closed automatically when the JavaScript object is garbagecollected.

https://stackoverflow.com/a/59796326/7259551

只需将 BrowserWindow 的实例设置为全局值即可解决此问题。

关于javascript - 将数据存储到系统时 Electron 应用程序崩溃(应用程序退出代码 3221225477),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72395356/

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