简体   繁体   English

在C ++中通过析构函数销毁动态数组的向量

[英]Destroying vectors of dynamic arrays via destructor in c++

I'm working on one of assignments to do with image manipulation (blending and zooming) and I've ran into a problem that I have hard time overcoming. 我正在处理与图像处理(混合和缩放)有关的一项任务,但遇到了我很难克服的问题。

Structure of my application 我的申请结构

Class Image 班级形象

  • rgb struct: contains floats (they're flattened pixels) and overloaded operators. rgb struct:包含浮点数(它们是扁平的像素)和重载的运算符。
  • pixels: a 1d array of pixels which is initialized via constructor to be h * w pixel:一维像素数组,通过构造函数将其初始化为h * w
  • Class destructor. 类的析构函数。

Destructor looks a little like this... 析构函数看起来像这样...

virtual ~Image()
{
    if (pixels != NULL)
        delete[] pixels;
}

Now I'm using another class called Filter which inherits from Image 现在,我使用另一个名为Filter的类,该类继承自Image

class Filter: public class Image 类过滤器:公共类图像

  • std::vector of Image imgStack; 图片imgStack的std :: vector; Container for images that I'm going to blend 我将要混合的图像的容器
  • std::vector of Rgb pixelBuffer; Rgb pixelBuffer的std :: vector; A container for the pixels for each images one pixel. 每个图像的像素的容器一个像素。 This is not dynamic so I'm not worried about deleting this. 这不是动态的,因此我不必担心删除它。

Destructor for the derived class. 派生类的析构函数。

~Blend()
{
    delete &imageStack;
}

what this part of my main.cpp looks like... 我的main.cpp的这一部分是什么样的...

while (userInput != 5)
{
    switch(userInput)
    case 1:
    {
        Blend *meanImage = new Blend(3263, 2505, 13);
        meanImage->writePPM("Mean_" + std::to_string(count) + ".ppm");//every iteration, the file name is unique
        meanImage->~Blend();
    }
}

In my main.cpp I'm basically running 13 images into the Blend object which stores the images in the vector container to do all my functionality on. 在我的main.cpp文件中,我基本上是在Blend对象中运行13张图像,该对象将图像存储在vector容器中以执行所有功能。 During the run time the space used is around 1.3GB, but since my object is in a loop (i have a menu for multiple functionality) the object doesn't usually leave the scope so the destructor isn't automatically called, so I call it manually like this; 在运行时,使用的空间约为1.3GB,但是由于我的对象处于循环状态(我有一个具有多种功能的菜单),因此该对象通常不会离开作用域,因此不会自动调用析构函数,因此我调用像这样手动 medianImage->~Blend(); medianImage->〜共混物(); Now the all the error says is that my application "has triggered a breakpoint" and that's it... Note, no breakpoint is found anywhere. 现在,所有错误都表明我的应用程序“已触发断点”,仅此而已...注意,在任何地方都找不到断点。 I'm aware that it's generally bad to use dynamic arrays because it causes all sorts of memory problems (if it's done by me), but I want to fix this just so I know how to solve these in the future. 我知道使用动态数组通常是不好的,因为它会引起各种各样的内存问题(如果这是我自己做的),但是我想解决这个问题,以便我知道将来如何解决。

If you have any questions of the code, I can post snippers. 如果您对代码有任何疑问,我可以发布狙击手。

Edit: here's an my Blend class. 编辑:这是我的Blend类。

#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
class Blend : public Image
{
private:
    std::vector<Image> imageStack;
    std::vector<Rgb*> pixelBuffer;//only works with pointers (no copying)
    int m_noOfImages;

    Rgb* getMedianFromSet();
    Rgb getMeanFromSet();
    Rgb getStdDev(Rgb meanPix);
public:
    Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
    {}
public:
    void stack(bool push = true);
    void meanBlend();
    void medianBlend();
    void sigmaClipping(float sigma = 0.5f);

    ~Blend()
    {
        delete &imageStack;
    }
};
#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
#include <memory>
class Blend : public Image
{
private:
    std::vector<Image> imageStack;
    // Changed to shared_ptr<T>  could use unique_ptr<T> depending on need.
    std::vector<std::shared_ptr<Rgb>> pixelBuffer;//only works with pointers (no copying)
    int m_noOfImages;

    Rgb* getMedianFromSet();
    Rgb getMeanFromSet();
    Rgb getStdDev(Rgb meanPix);
public:
    Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
    {}
public:
    void stack(bool push = true);
    void meanBlend();
    void medianBlend();
    void sigmaClipping(float sigma = 0.5f);

    // Clear Entire Buffer
    void cleanup() {
        // When using the heap with smart pointers
        for ( auto item : containerVariable ) {
            item.reset();
            item = nullptr;
        }
        containerVariable.clear();
    }

    // Remove Single Element
    void remove( unsigned idx ) {
        // Write function to remove a single element from vector
    }

    ~Blend()
    {
        // This is definitely wrong here: You are trying to delete a reference 
        // to a template container that is storing `Image` objects that
        // are on the stack. 
        // delete &imageStack;
    }
};

It is better to write a function to clean up memory, and to remove specific elements from containers when using dynamic memory than it is to use a class's destructor. 与使用类的析构函数相比,编写一个函数来清理内存并在使用动态内存时从容器中删除特定元素要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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