-2

I'm trying to instantiate an object from a static member function. I've defined:

class A
{
public:
    A( );
    A(int aParam);
    ~A( );
    static A* Creator( );
private:
    int m_nData;
};

And Implemented:

#include "A.h"

A::A()
{
}

A::A(int aParam)
{
    m_nData = aParam;
}

A::~A()
{
}

A* A::Creator()
{
    return new A(0);
}

It works ok, but have problem when deleting object:

#include "A.h"
#include <stdio.h>

int main(int argc, char** argv)
{
    A* pointer = A::Creator();

    delete pointer;
    return 0;
}

This example works as expected. I'm affraid the problem could be more complex. I'll try to give more details: I've a class similar to described here inside a dll library (Windows). When I call static function from my program, I get an error (when debugging): HEAP[program.exe]: Invalid address specified to RtlValidateHeap( 0000000000BA0000, 000000000264B3D0 ) Windows has triggered a breakpoint in program.exe.

This may be due to a corruption of the heap, which indicates a bug in program.exe or any of the DLLs it has loaded.

As I've tested, it seems that the newly created object it's being allocated in the stack.

Any help would be appreciated.

4

1 回答 1

0

我认为您缺少析构函数的实现。编译器应该警告你。使用 gcc 和前面提到的修复程序,它可以工作。

于 2012-05-07T15:49:29.033 回答