|
Thanks for the great information.
Zdeslav Vojkovic wrote: if an exception is allocated on the heap (using 'new' operator), it must be caught through a pointer.
Why must? Why cannot it be accessed through reference? I mean, you catch what is thrown. So if you throw the object by dereferencing the pointer, you may catch it through reference. Isnt this correct?
Zdeslav Vojkovic wrote: however, this is not a good practice because it is not obvious to the client code how to handle it, and it complicates the deleting of the allocated exception instance (who will call delete on it).
I can see the complication, which doesnt exist in Java due to its automatic garbage collection.
Zdeslav Vojkovic wrote: exceptions shoud be stack allocated, thrown, and caught by reference.
This is what puzzles me. If the exception is stack allocated, should we make sure the catch statement is in the same scope(braces) as that of the exception variable? Otherwise, how is the stack cleaned up when the control moves out of the scope?
Zdeslav Vojkovic wrote: MFC implements somewhat specific idiom, where exceptions are allocated on the heap and you have to call Delete() method of the CException class to destroy the instance
I see. What is the equivalent for the C++ "exception" class to free the object? Btw, this Delete method of CException might destroy the instance to free up resources, but is the object itself freed too (perhaps through 'delete this')?
thanks!
|
|
|
|
|
Britley wrote: Why must? Why cannot it be accessed through reference?
because T* can't be treated as T, while T& can (they are interchangeable).
T x;<br />
T& ref = x;
T* ptr = x;
Britley wrote: If the exception is stack allocated, should we make sure the catch statement is in the same scope(braces) as that of the exception variable
no, catch statement can be at any level higher than the current context. implementation of stack unwinding is left to compiler vendors, so there is no single answer for that. VC++ creates temporary object which will be thrown, and destroys it after the stack is cleaned up
Britley wrote: What is the equivalent for the C++ "exception" class to free the object?
delete operator.
Britley wrote: Btw, this Delete method of CException might destroy the instance to free up resources, but is the object itself freed too (perhaps through 'delete this')?
it depends. CException constructor takes a BOOL parameter which states whether it is an auto-delete exception. if set to true, Delete() does nothing (this is used for CException instances allocated on the stack). if it is set to false (for heap-allocated instances), Delete() must be called, and it will call 'delete this' internally.
|
|
|
|
|
Zdeslav Vojkovic wrote: because T* can't be treated as T, while T& can (they are interchangeable).
I meant this:
<br />
try {<br />
T *t = new T;<br />
throw *t;<br />
}catch(T& te) {<br />
}<br />
Anything wrong, other than the memory leak, ofcourse?
Zdeslav Vojkovic wrote: What is the equivalent for the C++ "exception" class to free the object
Zdeslav Vojkovic wrote: delete operator.
but for objects created on stack I guess the compiler frees it eventually, so we dont need to call the delete operator. Why do you need to call Delete on the MFC CException again? May be its MFC specific logic...
thanks!
|
|
|
|
|
Britley wrote: Anything wrong, other than the memory leak, ofcourse?
no, that's completely valid.
Britley wrote: Why do you need to call Delete on the MFC CException again
actually you don't, but the problem is that if you call an MFC function which throws a CException*, you don't necessarilly know how it was allocated, so you can't call 'delete ex' because it will crash for stack allocated object, and if you fail to call it for heap allocated object you have a memory leak. Therefore, you must call Delete(), which knows whether to destroy the object or not. if you are 100% sure that it is stack-allocated, you can omit Delete() call, but still you can't be sure if next version of MFC will behave differently and then you would have to modify the code to avoid memory leak.
|
|
|
|
|
Britley wrote: What is the equivalent for the C++ "exception" class to free the object?
The C++ Standard Library throws objects of classes derived from std::exception , not pointers to them, and you should catch them by reference. You don't free the object.
|
|
|
|
|
Zdeslav Vojkovic wrote: 3. any class/type can be exception tpye, but maybe you would like to have look at the 'exception' class, which is a base class for all exeption classes in standard c++ library
To avoid confusion since he is coming from a Java background: In C++ the class does not have to derive from Exception to be thrown. Any type can by thrown.
Jared Parsons
jaredp@beanseed.org
http://jaredparsons.blogspot.com/[^]
|
|
|
|
|
Britley wrote: I started using try/catch(...) in C++, but not sure what all can be done in the catch block.
1. Anything that is holy, practically.
Britley wrote: 1. What does the elipsis (...) mean?
(...) Catch any exception that occurs
Britley :->wrote: What else can be put here?
A known exception type can be put here which has to be caught. Eg: catch(CFileException Cfe)
Britley wrote: If so, how do you define an exception class? Is there an interface that we should implement?
There are a bunch of exceptions. You will not need to implement any interface. You can use any existing exception.
Britley wrote: 4. When we have the elipsis(...) how do you get the details of the exception?
There is one API call known as GetLastError for this purpose
Britley wrote: 5. Are there simple examples anywhere that demonstrate exceptions in C++ for Java programmers
<br />
TRY<br />
{<br />
CDatabase DB;<br />
CRecordset Rc;<br />
CString szSql = "SELECT * FROM MYTABLE WHERE AGE = 20"<br />
DB.ExecuteSql(szSql);<br />
}CATCH(CDatabaseException CDbe){ AfxMessageBox(CDBE->m_error));}<br />
I believe above things are right. I a newbie too, but have worked on a database module. I am sure you will get a bunch of replies and guidance.
Aljechin Alexander
|
|
|
|
|
Aljechin wrote: Eg: catch(CFileException Cfe)
it is a bad practice to catch exceptions by value (except for integral types)
Aljechin wrote: There is one API call known as GetLastError for this purpose
it will not return details of exception. it is a Win API to retrieve last Win32 error for the current thread.
also, microsoft specific TRY/CATCH should be avoided, and standard try/catch used instead, for reasons stated here[^]
|
|
|
|
|
Hi guies,
I am new to VC++.
I have to write a code which contain connect method and which connect's
to server.
Anyone can give me that procedure,Pl'z.(which contains connect method).
yuwraj
|
|
|
|
|
http://www.codeproject.com/internet/winsockintro02.asp
never say die
|
|
|
|
|
it would help if you could be more specific in your questions, what kind of server, etc.
if you are talking about connecting through sockets, have a look at this[^] article. also, try google, it should return thousands of hits on this topic
|
|
|
|
|
|
Yuwraj wrote: I have to write a code which contain connect method and which connect's
to server.
How about NetUseAdd() or WNetAddConnection2() ?
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
|
|
|
|
|
Hi,
I want to findout whether notepad.exe is running in taskmanager or not. if i create a process to open notepad.exe then i can find process id and close it, but i am not opening notepad.exe through process.
pls help me, waiting 4 ur reply
bye
trinadh
|
|
|
|
|
Here is an example...
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown";
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
else return;
}
else return;
printf( "%s (Process ID: %u)\n", szProcessName, processID );
CloseHandle( hProcess );
}
void main( )
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
}
Love Forgives--Love Gives--Jesus is Love <marquee direction="up" height="50" scrolldelay="1" step="1" scrollamount="1" style="background-color:'#44ccff'">
--Owner Drawn
--Nothing special
--Defeat is temporary but surrender is permanent
--Never say quits
--Jesus is Lord
|
|
|
|
|
thanks for giving me reply,
i try this code if i get any doubt i get back to u.
bye
trinadh
|
|
|
|
|
Thanks to MSDN
Love Forgives--Love Gives--Jesus is Love <marquee direction="up" height="50" scrolldelay="1" step="1" scrollamount="1" style="background-color:'#44ccff'">
--Owner Drawn
--Nothing special
--Defeat is temporary but surrender is permanent
--Never say quits
--Jesus is Lord
|
|
|
|
|
|
Error:Cannot open include file: 'psapi.h':
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
|
For header files other than my own, I use angle brackets <> instead of quotes. In any case, does the file exist on your machine?
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
|
|
|
|
|
My program needs to be synchronised with my data server.
I must do it into my program, in VC++ 6 (not in .NET).
I can not find any function in msdn which do it.
I've another solution,;) instead of synchronising, I need to get the system time of the server. But I can not found any function to do this.
Does someone has already realize it ?
Firejano
|
|
|
|
|
First off, what kind of data are you synchronising?
Instead of depending on the time of the server, can you not depend on the time of the content/file that you are trying to synchronise ?
-Prakash
|
|
|
|
|
I try to synchronise my computer system time, with server one.
Firejano
|
|
|
|
|