سؤال

I'm running into a weird bus error when trying to create an object in C++. This is my gdb backtrace when the program crashes:

#0  0xff146ff4 in _malloc_unlocked () from /usr/lib/libc.so.1
#1  0xff146e40 in malloc () from /usr/lib/libc.so.1
#2  0x24430 in __builtin_new (sz=128) at /usr/local/src/gcc-2.95.1/gcc/cp/new1.cc:84
#3  0x1e71c in FileHeader::Allocate (this=0x3f5d8, freeMap=0x3eea0, fileSize=5719)
    at ../filesys/filehdr.cc:63
#4  0x1f61c in FileSystem::Create (this=0x3d8b8, name=0xffbff8f3 "test", initialSize=5719)
    at ../filesys/filesys.cc:200
#5  0x1ffac in Copy (from=0xffbff8e4 "assignment 2.c", to=0xffbff8f3 "test")
    at ../filesys/fstest.cc:52
#6  0x15150 in main (argc=3, argv=0xffbff768) at ../threads/main.cc:116

The relevant line of code from filehdr.cc is:

IndirectHeader * s;
s = new IndirectHeader;

It crashes on the second line. I thought it might be that I wasn't explicitly using my own constructor, but adding one didn't seem to help. It seems to me like there's some other simple problem i'm not noticing but i haven't been able to find it.. Any advice would be appreciated.

هل كانت مفيدة؟

المحلول

What you're seeing in the backtrace is a crash allocating the memory to back your IndirectHeader. It hasn't even started constructing the object yet because it's still trying to allocate memory for it. Most likely there is a bug earlier in your program, that has corrupted the heap.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top