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
Solved

CInternetException not disposed automatically

Posted on 2014-09-04
9
206 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 229
Fully specialized class template function 21 142
Would like to move button in a function 3 79
C++ standard library based binary archive format 6 104
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…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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