简体   繁体   中英

Adding struct timespec to class (and seeminly no other type) causes EXEC_BAD_ACCESS

I am trying to determine the dt that my engine is running in using sys/time.h

I thought I might do this by storing the struct timespec as a private member of my Engine class however that causes an EXEC_BAD_ACCESS in a completely unrelated section of the code.

Oddly enough I can add private int, long, and void* arguments in its place without causing a crash.

Here are the relevant parts. A lot has been cut out

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);

};

Yes this is a heap allocated semaphore. It is that way for a very important reason.

Just adding last causes an EXEC_BAD_ACCESS in a method of EntityManager inside of its initialization code it mallocs a large chunk of memory and then allocates each object calling its defalut constructor like so:

for (int i = activeCount; i < realCount; i++) {
        char* entityStart = pool + sizeof(Entity) * i;

        new(entityStart) Entity(i+1);
}

And the EXEC_BAD_ACCESS shows up as being in the first line of the default Entity class that I made. The first line simply sets a property of the Entity.

This is very confusing to me, especially since types other than struct timespec can be added without issue. So I am wondering what might cause this. Do you have any ideas? Is it possible that this is a compiler issue of some sort?

Alright so I ended up resolving this and it may have been more than acouple things. If you are having this issue check for the following:

  • Are reconstructors being called when you dont expect them to
  • Are you initializing all fields that need it such as ints
  • Are you mixing malloc with posix_memalign (I stopped doing this but it could be completely fine i'm not sure)

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM