填充矢量引起的内存泄漏

时间:2016-10-28 16:32:16

标签: c++ memory-leaks

我的内存泄漏是由函数转换引起的。 该函数将一个非常大的3D点数据向量转换为另一个 其他3D-pointdata的矢量。

这是我的代码:

IPoint3D.h

struct IPoint3D
{
    virtual ~IPoint3D()
    {
    }

    float x, y, z;
    virtual std::string toString() = 0;
};

RealSenseCamera.h

class RealSenseCamera : public IRealSenseCamera 
{
public:
    RealSenseCamera();
    ~RealSenseCamera();

    CameraInitializationResult CreateSession(int width, int height, int fps) override;
    SetSensorConfigurationResult SetSensorConfiguration(pxcBool dsAutoExposure, pxcBool dsCropping, pxcI32 dsGain, pxcI32 laserPower, PXCCapture::Device::IVCAMAccuracy accuracy) override;

    bool IsReady() override;
    int GetFrameSize() override;
    void Destroy() override;
    std::vector<IPoint3D*> QueryVertices() override;

private:
    int _width, _height, _fps;
    PXCSession* _session;
    PXCSenseManager* _senseManager;
    PXCProjection* _projection;
    PXCCapture::Device* _device;

    std::vector<IPoint3D*> convert(std::vector<PXCPoint3DF32> source);
};

RealSenseCamera.cpp

RealSenseCamera::RealSenseCamera() : _width(0), _height(0), _fps(0), _session(nullptr), _senseManager(nullptr), _projection(nullptr), _device(nullptr)
{
}

RealSenseCamera::~RealSenseCamera()
{
}

CameraInitializationResult RealSenseCamera::CreateSession(int width, int height, int fps)
{
    _width = width;
    _height = height;
    _fps = fps;

    _session = PXCSession::CreateInstance();
    if (_session == nullptr)
        return CameraInitializationResult(false, "Failed to create camera session instance.");

    _senseManager = _session->CreateSenseManager();

    if (_senseManager == nullptr)
        return CameraInitializationResult(false, "Failed to create SenseManager.");

    _senseManager->EnableStream(PXCCapture::STREAM_TYPE_DEPTH, width, height, fps);
    _senseManager->Init();

    PXCCaptureManager* captureManager = _senseManager->QueryCaptureManager();

    if (captureManager == nullptr)
        return CameraInitializationResult(false, "Failed to create CaptureManager.");

    _device = captureManager->QueryDevice();

    if (_device == nullptr)
        return CameraInitializationResult(false, "Failed to create device.");

    return CameraInitializationResult(true, "Initialization successful.");
}

bool RealSenseCamera::IsReady()
{
    return _session != nullptr && _device != nullptr && _senseManager != nullptr && _senseManager->IsConnected();
}

int RealSenseCamera::GetFrameSize()
{
    return _width * _height;
}

SetSensorConfigurationResult RealSenseCamera::SetSensorConfiguration(pxcBool autoExposure, pxcBool cropping, pxcI32 gain, pxcI32 laserPower, PXCCapture::Device::IVCAMAccuracy accuracy)
{
    if (_device == nullptr)
        return SetSensorConfigurationResult(false, "Failed to find device.");

        _device->SetDSLeftRightAutoExposure(autoExposure);
        _device->SetDSLeftRightCropping(cropping);
        _device->SetDSLeftRightGain(gain);
        _device->SetIVCAMLaserPower(laserPower);
        _device->SetIVCAMAccuracy(accuracy);

        return SetSensorConfigurationResult(true, "New Configuration ist set.");
}

std::vector<IPoint3D*> RealSenseCamera::QueryVertices()
{
    std::vector<PXCPoint3DF32> vertices;

    if (_senseManager->AcquireFrame(true) >= pxcStatus::PXC_STATUS_NO_ERROR) {

        PXCCapture::Sample* _sample = _senseManager->QuerySample();
        PXCImage* depthImage = _sample->depth;

        vertices.resize(_width * _height);

        _projection = _device->CreateProjection();
        _projection->QueryVertices(depthImage, &vertices[0]);
    }

    _senseManager->ReleaseFrame();

    // konvertierung leider bisher nötig
    return convert(vertices);
}

// Neuer vector wird für Punktdaten angelegt
std::vector<IPoint3D*> RealSenseCamera::convert(std::vector<PXCPoint3DF32> source)
{
    int size = GetFrameSize();
    std::vector<IPoint3D*> target(size);

    for (int i = 0; i < size; i++)
    {
        target[i] = new PXCPoint3DF32Adapter(source[i]);
    }

    return target;
}

void RealSenseCamera::Destroy()
{
    if (_device != nullptr)
        _device->Release();


    if (_senseManager != nullptr)
    {
        _senseManager->Close();
        _senseManager->Release();
    }

    if (_session != nullptr)
        _session->Release();
}

有人能看到问题吗?

0 个答案:

没有答案