如果传递的指针为null,则placement new会调用构造函数吗?

Whi*_*paz 5 c++ mfc stl atl visual-studio-2010

我试图将vc7.1项目转换为vs2010,这是我从codeproject获得的.(这里是链接htt p://www.codeproject.com/KB/cpp/transactions.aspx?fid = 11253&df = 90&mpp = 50&noise = 3排序=位置&视图=膨化&FR = 1#xx0xx

但经过转换和修改后的配置.

我发现它调试失败,它在DrawIt.exe中的0x0028e7b9处显示未处理的异常:0xC0000005:访问冲突写入位置0x00000000.

错误行是这样的

data = new(Mm::Allocate(sizeof(DocData), sid)) DocData();
Run Code Online (Sandbox Code Playgroud)

而且功能

void* Allocate(size_t size, SPACEID sid)
{
    AUDIT

    Spaces::iterator s = spaces.find(sid);
    if (s == spaces.end())
        return NULL;

    Space& space = s->second;
    if (!space.transacting) 
        return NULL;

    size = max(size, sizeof(Free));

    // TODO: assert that "data" is allocated in space
    space.AssertData();

    // are there any more free chunks?
    if (!space.data->sFreeHead) {
        space.data->Insert(space.More(size));
    }

    AUDIT

    // find the first chunk at least the size requested
    Free* prev = 0;
    Free* f = space.data->sFreeHead;
    while (f && (f->size < size)) {
        prev = f;
        f = f->next;
    }

    AUDIT
    // if we found one, disconnect it
    if (f) {
        space.data->locTree.remove((size_t)f);

        if (prev) prev->next = f->next;
        else space.data->sFreeHead = f->next;

        f->next = 0;
        memset(&f->loc, 0, sizeof(f->loc));
    } else {
        f = space.More(size);
    }

    // f is disconnected from the free list at this point

    AUDIT

    // if the free chunk is too(?) big, carve a peice off and return
    // the rest to the free list
    if (f->size > (2*(size + sizeof(Free)))) {
        Free* tmp = space.data->Slice(f, size); // slice size byte off 'f'
        space.data->Insert(f); // return the remainder to the free list
        f = tmp;
    }

    AUDIT

    CHECK_POINTER(f)

    void* p = reinterpret_cast<void*>((char*)f + sizeof(Free::SIZE_TYPE));

    CHECK_POINTER(p)

    return p;
}
Run Code Online (Sandbox Code Playgroud)

任何人都有想法,PLZ?

由于我不擅长C++,所以在我弄清楚如何解决这个问题之前需要一些时间.刚刚上传了源代码源文件,如果有人可以提供帮助,我们将不胜感激.

Pet*_*der 1

好吧,你的Allocate函数显然正在返回NULL. 我们很难说出在哪里以及为什么,而且您自己设置断点并单步执行分配器也很简单,所以我建议您这样做并找出函数返回的位置。