ensuring statically allocated memory is destroyed

I have some BYTE* buffer's defined statically in my camera class (for performance reasons)

BYTE* CMyCamera::m_pTempFrameBuffer      = new BYTE[iHeight * iWidth];            // 8bit buffer for raw data
BYTE* CMyCamera::m_pBuffer      =  new BYTE[iHeight * iWidth * iByteStep];      // 24bit buffer for RGB converted data

So these arrays are created, even if CMyCamera is never instantiated.  In the cases where CMyCamers is instantiated, I delete[] and NULL out those buffers in the destructor.

But there are cases where my app never instantiates CMyCamera (if the user never uses it) - so howthen do I ensure they are destroyed to prevent the memory leak?

ie. how do I destroy statically allocated memory like that?

thanks!
PMH4514Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
When your program exits, the memory will be freed anyway.

But, you could use a helper class like e.g.

BYTE* CMyCamera::m_pTempFrameBuffer     = new BYTE[iHeight * iWidth];          // 8bit buffer for raw data
BYTE* CMyCamera::m_pBuffer     =  new BYTE[iHeight * iWidth * iByteStep];     // 24bit buffer for RGB converted data

struct CMyCameraCleanupHelper {

CMyCameraCleanupHelper () {}
~CMyCameraCleanupHelper () {

delete [] CMyCamera::m_pBuffer;
delete [] CMyCamera::m_pTempFrameBuffer;

}

};

CMyCameraCleanupHelper g_hlp;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterCommented:
If you're accessing these buffers via global object's destructor, it's better if you don't delete the buffer at all.

>>even if CMyCamera is never instantiated.
You could avoid creating the buffer when it's not needed by putting it in a static function.
Example:
BYTE* CMyCamera::get_pTempFrameBuffer()
{
   static BYTE* pTempFrameBuffer     =  new BYTE[iHeight * iWidth];  
   return pTempFrameBuffer;
}

If this function is never called, then the buffer is never crated.
0
Deepu AbrahamR & D Engineering ManagerCommented:
To delete the buffer you can try checking the buffer pointer.

like,

if( ptrBuffer!=NULL)
{
 delete [] pTempFrameBuffer;

}

Best Regards,
Deepu
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Deepu AbrahamR & D Engineering ManagerCommented:
Sorry,

Its a copy paste error :(

To delete the buffer you can try checking the buffer pointer inside the destructor.

like,

CMyCamera:: ~CMyCamera()
{
  if( ptrBuffer!=NULL)
 {
  delete [] ptrBuffer;
 }

}

Best Regards,
Deepu
0
AxterCommented:
>>To delete the buffer you can try checking the buffer pointer.

There is no need to check the buffer pointer.
IAW C++ standard you can call delete on a NULL pointer, so delete is already performing this logic.
By adding an extra check, it just makes the code less efficient, and adding un-needed code.

Instead, just call delete directly.
delete [] pTempFrameBuffer; //No pre-check for NULL
0
bachra04Commented:
Hi,

your buffer is not statically allocated, it is dynamically allocated at the heap memory,  so you need to destroy it using delete[].
The right way to do that is to delete it when you don't need it anymore at the correct point. So if you allocate the buffer at the constructor then you need to delete it at the destructor but if it is allocated at the start of the program i.e globally, then you need to destroy it at the end of the program.
I think if the performance is your issue the best way is to declare the buffer at the stack memory like this you don't have any problem with memory :
const int MAX_SIZE = 1000;

BYTE CMyCamera::m_pTempFrameBuffer [MAX_SIZE ];          

B.T
0
i_maheshCommented:
Hi,
there are may approaches you can take to tackel this issue:

There are two conditions
a. You know the size of the buffer to be allocated at compile time
b. You will get to know the size of the buffer only after you start running the program.

in case a. the best option would be to take the approach suggested by bachra04 ie.
you can allocate the memory on the global / static stack instead of dynamically creating the buffer at run time provided the size of the buffer is known at compile time.

in case b. you have the following options;

1. As suggested by jkr.
2. you can initialize the static buffer pointers to NULL and allocate memory in the constructor when forst object instance of the class is created and delete the buffer when last instance of the object is being destroyed. for this approach you need to do reeference counting, for which you can include a static member in your class which will keep the track of numbers of instances of the class you.

the things will look some thing like this:

class CMyCamera
{
public:
    CMyCamera()
   {
       if (m_cRef == 0)
       {
            m_pTempFrameBuffer     = new BYTE[iHeight * iWidth];
            m_pBuffer     =  new BYTE[iHeight * iWidth * iByteStep];
        }
        m_cRef++;
   }

    ~CMyCamera()
   {
       m_cRef--;
       if (m_cRef == 0)
       {
            delete [] m_pTempFrameBuffer;
            delete[] m_pBuffer;
            m_pTempFrameBuffer = NULL;
            m_pBuffer = NULL;
        }
    }

static BYTE* m_pTempFrameBuffer;          // 8bit buffer for raw data
static BYTE* m_pBuffer;     // 24bit buffer for RGB converted data
static UINT m_cRef;
};

BYTE* CMyCamera::m_pTempFrameBuffer     = NULL;          // 8bit buffer for raw data
BYTE* CMyCamera::m_pBuffer     =  NULL;     // 24bit buffer for RGB converted data
UINT CMyCamera::m_cRef = 0;   //Reference count for the class

0
PMH4514Author Commented:
interesting stuff.

I do know the sizes required at compile time. I used to allocate the memory on the non-static members in the constructor and delete it in the destructor. worked just fine. I noticed some performance improvements when I defined them as static and allocated the memory "statically" (that is, at the top of my class, outside the code block, I called the new BYTE .....) though I admit I don't fully understand why there would be a performance improvement.

my thinking is that "these sizes are fixed, and known at compile time. how can I best setup the buffers for use and ensure they're deleted on program exit"
0
AxterCommented:
>>I best setup the buffers for use and ensure they're deleted on program exit"

You don't need to.  On program exit, the buffer is release automatically.
0
PMH4514Author Commented:
>>On program exit, the buffer is release automatically.

ok that's what I thought. I was seeing the IDE show memory leaks on those two buffers though, but that was on abnormal program termination.
0
bachra04Commented:
If you know the size at compile time why you allocate the buffer using NEW, I don't think it is a good practice, in addition it makes your program heavier (more heap memory used).

B.T

0
PMH4514Author Commented:
>>why you allocate the buffer using NEW, I don't think it is a good practice,

what is the appropriate alternative?
0
bachra04Commented:
You need to declare a static array like this:

in the class definition declare a static array member with a fixed size.

const unsigned int  MAX_SIZE = 1000;
Class Camera
{

public:

//....

static Byte m_FrameArray[MAX_SIZE];

}

like this your array will be allocated at compile time and not at run time, this improves performance and avoid you any memory problems.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.