Solved

CInternetException not disposed automatically

Posted on 2014-09-04
9
209 Views
Last Modified: 2014-09-22
I have written a piece of code, that is calling a URL and storing the input in a CString. All this is done inside a try-catch(...) block.

When checking for memory leaks I have noticed, that CInternetException objects were remaining in memory. So I had to modify the catch to

catch (CInternetException *e)
{ e->Delete();
}

I don't understand why this is necessary. Is there anything special about the CInternetSession and CHttpConnection objects, so that the undefined catch is not disposing the exception objects automatically?

Thanks in advance!
0
Comment
Question by:WH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40303389
are you calling close() on the Internet session you create?
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40303405
I don't know what you mean by undefined catch but I'm assuming you mean the catch all block

you can't expect the catch(...) to clean up memory. It catches all exceptions. Within a catch block you can do anything you please, rethrow the exception, ignore it or transform it in someway
The compiler does not generate code to look out for a CInternetException object if you don't tell it to. So you need to catch the CInternetException object by reference and even then you can't expect to clean itself up unless you explicitly do it via a call to delete
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40303409
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WH
ID: 40303498
I still don't understand why after an exception was caught by a catch all block the exception object is remaining in memory after leaving the catch block. In the examples on the Microsoft site the CInternetException isn't handled with delete before leaving the catch block. Before disposing the Internet session object I am closing the session.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40304140
>>I still don't understand why after an exception was caught by a catch all block the exception object is remaining in memory
>>after leaving the catch block.

That is because a 'CInternetException*' (a pointer) is thrown and caught. If that was a 'CInternetException&', the 'Delete()' call wouldn't be necessary, but at the time WinInet and the corresponding MFC classes were conceived, C++ was just closing in to be standardized (1998). I am pretty sure that the guys at MS nowadays would use a 'CInternetException&', but the pointer consrtuct is kept for backwards compatibility.
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40305247
jkr is right.

if you throw by pointer then throw a smart pointer else the recommended way is to throw by value and catch by reference
0
 

Author Comment

by:WH
ID: 40305253
Thanks jkr!

How do I dispose exceptions caught in a catch all (catch(...)) block?
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40305274
if you catch by reference then the compiler takes care of doing the cleanup.

A catch all block shouldn't be the only catch block in the catch hierarchy. Still if you don't know what exceptions to catch like I said you will need to wrap your exceptions using a guard pattern or a smart pointer so that they get cleaned up appropriately
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 500 total points
ID: 40305280
typically the ellipsis catch handler (...) should only be used to rethrow the exception to a more appropriate handler
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question