user565739
user565739

Reputation: 1332

delete pointers created by new in CallBack Function

I am doing the following:

LRESURT CALLBACK WindowProc{
    .......
    Case VK_RETURN:
        int i;
        for ( i = 0; i <1000; i++) {
            int  size = determinesize(i);
            int *pttest = new int[size];
            .....(some work)....
            delete[] pttest;
        }
     ........
}

But I got problem when VK_RETURN occurs. If I don't use delete[] pttest.

My question is that: Do I need to delete ptttest? When I search on google, almost everyone said that for each new, there must be a delete. Another question is that: if I put the for loop code for VK_RETURN in a function, say void whenvkreturn(), then do I need to delete pttest? Since any local variables are destroyed after returning, I guess the 1000 objects created in this loop will be deleted automatically, right? I don't know the stack very well, so some explanation I found is not clear for me.

=====

OK, there is a vector workaround. But when it is the case:

TCHAR *text = new text[size];

What I am supposed to do?

Upvotes: 0

Views: 701

Answers (5)

Adrian Dorr
Adrian Dorr

Reputation: 11

As a rule you should never use new[] these days and certainly not delete. Instead use vector. For extra efficiency points reuse the same vector resize it tto avoid allocating 1000 separate arrays:

vector<int> test;

for(int i = 0; i < 1000; ++i) {
    test.resize(determinsize(i));
    ...
}

Upvotes: 1

Component 10
Component 10

Reputation: 10497

If you leave the code as it is then you don't need to use delete, or new for that matter:

int size = determinesize(i);
std::vector<int> test( size );
.....(some work)....

a stack based solution will work just as well. If you want to leave it as it is then of course you don't need to delete pttest at this point but you'll have to delete it at some point and taking it out of the switch increases the risk that it won't work correctly and will lean.

Upvotes: 1

Hakan Serce
Hakan Serce

Reputation: 11256

When you create a pointer using new or new[] operations in C++ you allocate memory in the heap. This is the case even if you do this in a method. So you need to use delete or delete[] as appropriate in order to deallocate the heap memory.

Upvotes: 1

user784668
user784668

Reputation:

Regarding your question, yes, you need to delete pttest, unless you want to intentionally leak memory. Basically, whenever you're allocating something with new/new[], you're supposed to delete/delete[] it manually later when you don't need it anymore.

But you really should consider using std::vector instead of an array allocated with new[]. You won't then have to worry about freeing the memory with delete[], std::vector will do that automatically in its destructor.

Upvotes: 1

Martol1ni
Martol1ni

Reputation: 4702

You always need to delete allocated memory. For every new there should be a delete, even though it is in a function. The problem you are approaching, can easily be solved by using other containers with dynamic sizes, like std::vector. Using dynamic containers in local functions, it will automatically release the memory allocated by itself, and you don't have to worry.

Upvotes: 1

Related Questions