Solved

CInternetException not disposed automatically

Posted on 2014-09-04
9
214 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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