Click here to Skip to main content
16,004,828 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Compiler Error Pin
T.RATHA KRISHNAN4-Aug-08 23:01
T.RATHA KRISHNAN4-Aug-08 23:01 
AnswerRe: Compiler Error Pin
Mark Salsbery5-Aug-08 6:23
Mark Salsbery5-Aug-08 6:23 
QuestionHow to get camera properties? Pin
Shivarudrayya H4-Aug-08 20:30
Shivarudrayya H4-Aug-08 20:30 
AnswerRe: How to get camera properties? Pin
Mark Salsbery5-Aug-08 6:26
Mark Salsbery5-Aug-08 6:26 
Questiontry-catch dynamic memory cleanup Pin
x87Bliss4-Aug-08 20:21
x87Bliss4-Aug-08 20:21 
AnswerRe: try-catch dynamic memory cleanup Pin
pallaka4-Aug-08 20:39
pallaka4-Aug-08 20:39 
AnswerRe: try-catch dynamic memory cleanup Pin
Cedric Moonen4-Aug-08 20:39
Cedric Moonen4-Aug-08 20:39 
AnswerRe: try-catch dynamic memory cleanup Pin
not_in_use5-Aug-08 8:33
not_in_use5-Aug-08 8:33 
Objects allocated on the heap (i.e. via new) are not freed automatically. Objects allocated on the stack (like MyObject obj;) are freed when they go out of scope (i.e. when the execution leaves the innermost {} that surrounds the declaration) or when an exception is thrown. So in your example, if failfunc() throws an exception other than an integer (which can easily happen when someone changes failfunc() but not main()), you got yourself a memory leak.

However, you can use what's called a smart pointer to get the desired behavior. The smart pointer is is a helper class that is allocated on the stack and behaves like a regular pointer, and when an exception is thrown its destructor gets called. In that destructor, it calls delete[] on its pointee. Just google smart pointers.

Example (not tested):

//
// Smart pointer class for arrays
//
template< typename T >
class ArrayPtr
{

public:

    ArrayPtr( T* pointee )
        : _pointee( pointee )
    {
    }

    inline ~ArrayPtr() {
        if( _pointee )
            delete[] _pointee; // Array destruction
    } 

    operator T*() {
        return _pointee
    }

    // ... more needed here. Just google smart pointers.
    // Basically you need operator->, operator*, but not operator&
    // plus assignment etc.

private:

    T* _pointee;

};

//
// How to use it in your example
//
void failfunc()
{
    throw 1; // obviously not practical, just for example
}

void main()
{
    try {
        ArrayPtr< char > mystr( new char[512] ); // Freed automatically when mystr goes out of scope or an exception is thrown
        failfunc();
    } catch( int err ) {
        // Tell the user something went wrong
        std::cerr << "Error #" << err << std::endl;
    }
}


Or if you don't need to print out an error message:
template< typename T > class ArrayPtr { /*... see above*/ };

void failfunc() { /*... see above*/ }

void main()
{
    ArrayPtr< char > mystr( new char[512] ); // Freed automatically when mystr goes out of scope or an exception is thrown
    failfunc();
}


Note that you cannot use the standard C++ std::auto_ptr for arrays since its destructor only calls delete, not delete[]. This also implies that you need a separate smart pointer type for data allocated with malloc since it must be freed using free, not delete[].

I suggest that you use smart pointers, otherwise you'll end up having loads of manual try...catch blocks just for ensuring that memory is freed properly, which is hard to maintain and leads to bugs. Also note that the version of main() using the smart pointer is much shorter than your original one since you don't need all the cleanup code.

If sombody now changed failfunc() to throw something else than an integer, your memory would still be freed correctly since the smart pointer would take care of that.

Hope that helps
Peter
GeneralRe: try-catch dynamic memory cleanup Pin
x87Bliss5-Aug-08 11:25
x87Bliss5-Aug-08 11:25 
GeneralRe: try-catch dynamic memory cleanup Pin
not_in_use5-Aug-08 12:07
not_in_use5-Aug-08 12:07 
AnswerRe: try-catch dynamic memory cleanup Pin
Stephen Hewitt5-Aug-08 14:05
Stephen Hewitt5-Aug-08 14:05 
QuestionHow to install MyApp.exe from custom actions in setup and deployment type project in VS2005 Pin
kapardhi4-Aug-08 20:09
kapardhi4-Aug-08 20:09 
AnswerRe: How to install MyApp.exe from custom actions in setup and deployment type project in VS2005 Pin
_AnsHUMAN_ 4-Aug-08 20:26
_AnsHUMAN_ 4-Aug-08 20:26 
AnswerRe: How to install MyApp.exe from custom actions in setup and deployment type project in VS2005 Pin
KarstenK4-Aug-08 21:19
mveKarstenK4-Aug-08 21:19 
QuestionHow to set Debug Info option in vc++6.0 for command line build? Pin
Super Hornet4-Aug-08 19:48
Super Hornet4-Aug-08 19:48 
QuestionRe: How to set Debug Info option in vc++6.0 for command line build? Pin
Super Hornet4-Aug-08 21:25
Super Hornet4-Aug-08 21:25 
QuestionHow to change Regional Settings(API)? Pin
ritz12344-Aug-08 19:36
ritz12344-Aug-08 19:36 
Questioncreate data base in vc++6 Pin
ani_ikram4-Aug-08 18:27
ani_ikram4-Aug-08 18:27 
AnswerRe: create data base in vc++6 Pin
_AnsHUMAN_ 4-Aug-08 20:00
_AnsHUMAN_ 4-Aug-08 20:00 
GeneralRe: create data base in vc++6 Pin
ani_ikram4-Aug-08 20:56
ani_ikram4-Aug-08 20:56 
GeneralRe: create data base in vc++6 Pin
_AnsHUMAN_ 4-Aug-08 21:46
_AnsHUMAN_ 4-Aug-08 21:46 
QuestionAn exception occurred while trying to run "Shell32.dll,Control_RunDLL ConnectorCpl.cpl Pin
monsieur_jj4-Aug-08 17:23
monsieur_jj4-Aug-08 17:23 
QuestionLINK : fatal error LNK1104: Pin
ani_ikram4-Aug-08 17:23
ani_ikram4-Aug-08 17:23 
AnswerRe: LINK : fatal error LNK1104: Pin
sudhir_Kumar4-Aug-08 19:20
sudhir_Kumar4-Aug-08 19:20 
QuestionClient rectangle not actualized after menu removal Pin
FloatingMarc4-Aug-08 16:40
FloatingMarc4-Aug-08 16:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.