Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3046

malloc in c++ class, is it getting deallocated

$
0
0
Hello,

I am trying to update a test program class, to my program

In that, I can see a buffer is allocated with malloc. But doesnot call free anywhere !. It looks like going to be memry leak. Am i right ?

Code:

#pragma once
#include <string>
#include <filesystem>
#include "algrithm/general/pnt2d.h"
#include "windows.h"
#include <math.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/algorithm/string.hpp>

using namespace std;

class DumpBilHelper
{
       
public:
        float *buffer;
       
        float EastMin, EastMax, NorthMin, NorthMax, binSize, E, N;
        size_t nRows, nCols, iR, iC;
        size_t nBins;
        string BYTEORDER, LAYOUT, PIXELTYPE;
        string wktProjection;
        long nBands, nBits, BandRowBytes, TotalRowBytes, UTMZone;
        float NoData;
        bool HeaderOK;
        string fnBIL;

        DumpBilHelper();

        DumpBilHelper(float inEastMin, float inEastMax, float inNorthMin, float inNorthMax, int inUTMZone, float inBinSize);
       
        void Init(float inEastMin, float inEastMax, float inNorthMin, float inNorthMax, int inUTMZone, float inBinSize);
        void SaveBIL(string FileName);
        void LoadHeader(string FileName);
       
        void SetEN(float E, float N, float value);
        void SetEN(float E, float N);
        void SetRC(int R, int C, float value);
        void AddRC(int R, int C, float value);
        float GetEN(float E, float N);
        float GetRC(int R, int C);
        void Clear();
};

In the .cpp

Code:

#include "DumpBilHelper.h"

DumpBilHelper::DumpBilHelper()
{

}
DumpBilHelper::DumpBilHelper(float inEastMin, float inEastMax, float inNorthMin, float inNorthMax, int inUTMZone, float inBinSize)
{
        this->Init(inEastMin, inEastMax, inNorthMin, inNorthMax, inUTMZone, inBinSize);
}

void DumpBilHelper::Init(float inEastMin, float inEastMax, float inNorthMin, float inNorthMax, int inUTMZone, float inBinSize)
{       
        /// initialize ESRI BIL array and metadata
        binSize = inBinSize;
        UTMZone = inUTMZone;

        EastMin = floor(inEastMin / binSize) * binSize;
        EastMax = floor(inEastMax / binSize) * binSize;
        NorthMin = floor(inNorthMin / binSize) * binSize;
        NorthMax = floor(inNorthMax / binSize) * binSize;
        NoData = -9999.0f;

        nRows = (NorthMax - NorthMin) / binSize + 1;
        nCols = (EastMax - EastMin) / binSize + 1;
        BandRowBytes = nCols * sizeof(float);
        TotalRowBytes = BandRowBytes;
        nBits = sizeof(float)*8;
        nBins = nRows * nCols;
       
//        BOOST_LOG_TRIVIAL(debug) << "Malloc " << nBins << " " << nRows << "x" << nCols;
        buffer = (float *) malloc(nBins * sizeof(float));
        if (buffer == NULL)
        {
//                BOOST_LOG_TRIVIAL(error) << " Malloc failed for " << (size_t)(nBins * sizeof(float) / 1024 / 1024 / 1024) << " GB";
                throw("Malloc failed");
        }
        this->Clear();
}

void DumpBilHelper::SaveBIL(string Filename)
{
        ofstream HDR, fBIL;
        string sBIL, sHDR;
//        BOOST_LOG_TRIVIAL(debug) << "Range: " << fixed << setprecision(2) << EastMin << " " << EastMax << " " << NorthMin << " " << NorthMax << ", Size: " << nRows << "x" << nCols << ", Resolution " << binSize << ", NODATA "<< NoData;
       
        sHDR = Filename;
        sHDR.append(".hdr");
        //BOOST_LOG_TRIVIAL(debug) << "Creating ESRI BIL header file " << sHDR;
        HDR.open(sHDR);

        HDR << "BYTEORDER            I" << endl;
        HDR << "LAYOUT              BIL" << endl;
        HDR << "NROWS                " << nRows << endl;
        HDR << "NCOLS                " << nCols << endl;
        HDR << "NBANDS              1" << endl;
        HDR << "NBITS                32" << endl;
        HDR << "BANDROWBYTES        " << nCols * sizeof(float) << endl;
        HDR << "TOTALROWBYTES        " << nCols * sizeof(float) << endl;
        HDR << "PIXELTYPE            FLOAT" << endl;
        HDR << "ULXMAP              " << fixed << setprecision(2) << EastMin + binSize / 2 << endl;
        HDR << "ULYMAP              " << NorthMax - binSize / 2 << endl;
        HDR << "XDIM                " << binSize << endl;
        HDR << "YDIM                " << binSize << endl;
        HDR << "NODATA              " << NoData << endl;

        HDR.close();

        // saving raster data file
        sBIL = Filename;
        sBIL.append(".bil");
//        BOOST_LOG_TRIVIAL(debug) << "Creating ESRI BIL data file " << sBIL;

        fBIL.open(sBIL, ios::out | ios::binary);
        fBIL.write((char *)buffer, nRows*nCols * sizeof(float));
        fBIL.close();
}


void DumpBilHelper::Clear()
{
        for (iC = 0; iC < nRows*nCols; iC++)
                buffer[iC] = 0;

}
void DumpBilHelper::SetEN(float E, float N, float value)
{
        SetEN(E, N);
        // cout << E << '\t' << N << '\t' << iR << '\t' << iC << endl;
        SetRC(iR, iC, value);       
}
void DumpBilHelper::SetEN(float inE, float inN)
{
// just calculating row and column, positioning "cursor" on it, no change
        iR = (NorthMax - inN ) / binSize;
        iC = (inE - EastMin ) / binSize;
        E = floor(inE / binSize) * binSize + binSize / 2;
        N = floor(inN / binSize) * binSize + binSize / 2;

}

void DumpBilHelper::SetRC(int R, int C, float value)
{
        if ((R <= nRows) && (C <=nCols) && (R>=0) && (C>=0) )
                buffer[R*nCols + C] = value;
}

void DumpBilHelper::AddRC(int R, int C, float value)
{
        if ((R <= nRows) && (C <= nCols) && (R >= 0) && (C >= 0))
                buffer[R*nCols + C] += value;
}

float DumpBilHelper::GetEN(float E, float N)
{
        iR = (NorthMax - N) / binSize;
        iC = (E - EastMin) / binSize;
        return GetRC(iR, iC);
}

float DumpBilHelper::GetRC(int R, int C)
{
        if ((R <= nRows) && (C <= nCols))
        {
                E = EastMin + C * binSize + binSize / 2;
                N = NorthMax - R * binSize - binSize / 2;
                return buffer[R * nCols + C];
        }
        else
                return NAN;
               
}

void DumpBilHelper::LoadHeader(string FileName)
{
        filesystem::path p(FileName);
        string hdrFileName, Line;
        vector<string> tokens;
       
        fnBIL = FileName;

       

//        BOOST_LOG_TRIVIAL(info) << "Using coverage file " << FileName;

        hdrFileName = p.replace_extension(".hdr").string();
       
        ifstream HDR;
        HDR.open(hdrFileName);
        while (getline(HDR, Line))
        {
                // convert to the uppercase, and split into attribute/value pair
                transform(Line.begin(), Line.end(), Line.begin(), ::toupper);
                boost::split(tokens, Line, boost::is_any_of("\t "), boost::token_compress_on);
               
                if (tokens.size() == 2)
                {
                        // as expected
                        if (tokens[0] == "BYTEORDER")
                        {
                                BYTEORDER = tokens[1];
                                continue;
                        }
                        if (tokens[0] == "LAYOUT")
                        {
                                LAYOUT = tokens[1];
                                continue;
                        }
                        if (tokens[0] == "NROWS")
                        {
                                nRows = stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "NCOLS")
                        {
                                nCols = stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "NBITS")
                        {
                                nBits = stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "PIXELTYPE")
                        {
                                PIXELTYPE = tokens[1];
                                continue;
                        }
                        if (tokens[0] == "ULXMAP")
                        {
                                EastMin= stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "ULYMAP")
                        {
                                NorthMax = stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "XDIM")
                        {
                                binSize = stoi(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "NODATA")
                        {
                                NoData = stof(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "TOTALROWBYTES")
                        {
                                TotalRowBytes = stol(tokens[1]);
                                continue;
                        }
                        if (tokens[0] == "BANDROWBYTES")
                        {
                                BandRowBytes = stol(tokens[1]);
                                continue;
                        }
                }
                else
                {
                }
//                        BOOST_LOG_TRIVIAL(error) << "Invalid ESRI HDR line " << Line;
        }
        //cout << "DBG " << EastMin << "," << NorthMax << "," << nRows << "," << nCols << "," << binSize << endl;
        HDR.close();
        HeaderOK = true;

}

I am thinking of adding the explicit destructor with the free to buffer ?

Any comments are very helpful.

Thankyou
pdk

Viewing all articles
Browse latest Browse all 3046

Trending Articles