![](/img/trans.png)
[英]unsigned char inbuf[1024000] throw an EXEC_BAD_ACCESS
[英]Adding struct timespec to class (and seeminly no other type) causes EXEC_BAD_ACCESS
我正在嘗試使用sys/time.h
確定我的引擎正在運行的 dt
我想我可以通過將 struct timespec 存儲為我的 Engine 類的私有成員來做到這一點,但這會在代碼的完全不相關的部分中導致 EXEC_BAD_ACCESS 。
奇怪的是,我可以在其位置添加私有 int、long 和 void* 參數而不會導致崩潰。
以下是相關部分。 刪了很多
class Engine {
private:
static Engine* instance;
Engine();
struct timespec last;
public:
static Engine* shared() {
if (!instance) {
instance = new Engine();
}
return instance;
}
Engine(const Engine&) = delete;
// EntityManager
EntityManager entityManager;
bool setCount(int num);
};
是的,這是一個堆分配的信號量。 這樣做有一個非常重要的原因。
僅在其初始化代碼內的 EntityManager 方法中添加 last 會導致 EXEC_BAD_ACCESS,它會分配一大塊內存,然后分配每個對象調用其默認構造函數,如下所示:
for (int i = activeCount; i < realCount; i++) {
char* entityStart = pool + sizeof(Entity) * i;
new(entityStart) Entity(i+1);
}
EXEC_BAD_ACCESS 顯示在我創建的默認實體類的第一行中。 第一行簡單地設置實體的屬性。
這讓我很困惑,尤其是因為可以毫無問題地添加 struct timespec 以外的類型。 所以我想知道什么可能導致這種情況。 你有什么想法? 這可能是某種編譯器問題嗎?
好吧,所以我最終解決了這個問題,這可能不僅僅是幾件事。 如果您遇到此問題,請檢查以下內容:
祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.