Solved

CInternetException not disposed automatically

Posted on 2014-09-04
9
194 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

758 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

17 Experts available now in Live!

Get 1:1 Help Now