Solved

CInternetException not disposed automatically

Posted on 2014-09-04
9
199 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now