|
Hi George,
You are nto giving up - that's good
I overlooked the comment for extra code that might throw exceptions.
With that code, you got yourself in a very ugly situation:
If you catch the (...) exception, where was it thrown? by a base class, or a member constructor? IUn that case, you cannot even legally test p!=0 (it is likely randomly initialized, anyway, and the compiler might generate code that causes an access violation at runtime).
What you need to do is this:
class A
{
private:
int* p;
public:
A() try
{
try
{
p = NULL;
p = new int[10];
}
catch (...)
{
}
}
catch (...)
{
}
}
|
|
|
|
|
Hi peterchen,
To answer your questions,
1. where are the exceptions from? Two sources, bad_alloc from new and some other places after we successfully allocated memory for p.
Here is my code,
// ... there are some other exceptions here
peterchen wrote: If you catch the (...) exception, where was it thrown?
2. about value of p
If new is successful, surely we can use !p to check whether it is ok to free memory. If it is bad_alloc exception, I will never delete it.
peterchen wrote: IUn that case, you cannot even legally test p!=0 (it is likely randomly initialized, anyway, and the compiler might generate code that causes an access violation at runtime).
If there is anything wrong in my code, please feel free to correct me.
regards,
George
|
|
|
|
|
Re-read the GotW article again. Your issues are addressed in it.
Specifically,
'"...to free resources allocated in the initializer list or in the constructor body!"
Sorry, nope. After all, remember that once you get into your constructor try-block's handler, any local variables in the constructor body are also already out of scope, and you are guaranteed that no base subobjects or member objects exist any more, period. You can't even refer to their names.'
This means p may not be referenced in the exception handler. The object does not exist. The object members do not exist.
|
|
|
|
|
Hi Member 754960,
I will delete p in the local try block handler and re-throw the exception, other than function try block handler. Do you think this way is ok?
regards,
George
|
|
|
|
|
Rethrowing the exception is fine.
Trying to delete p would be an error.
You cannot address p.
It doesn't exist.
Forget it, it's lost.
So is the memory allocated to initialize it.
So, don't do it this way.
Do what the article recommends: Use a container object.
|
|
|
|
|
No Member 754960,
We can access in local function try block, but not in function try block -- they are two different things.
Here is the guideline from GotW
http://www.gotw.ca/gotw/066.htm[^]
--------------------
Moral #4: Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.
--------------------
If you have any issues, please feel free to let me know.
regards,
George
|
|
|
|
|
The original question was in reference to the function try-catch handler.
If you want to use a local try catch block in the ctor body, as the GotW indicates, to handle the unmanaged memory, fine.
|
|
|
|
|
Thanks Member 754960,
I agree. Cool.
regards,
George
|
|
|
|
|
I try to convert my old MFC application that use multibye encoding to UNICODE by using TCHAR programming method.
but there is a code the create a file using ofstream that I dont know how to convert to TCHAR-programming.
I also tried std::wofstream
<br />
wofstream outFile(_T("outfile.txt"));<br />
outFile<<_T("Test international character : ???????,??????\n");<br />
outFile.close();<br />
But it didn't work , only the english portion is written to the output file (and the file are still ASCII as open in notepad).
Can someone suggest me how to fix the problem ?
Another question what is length in function like GetWindowText and GetCurrentDirectory are.Is it number of character or a number of byte ?
Edit : those ?????? are suppose to be thai and arabic text
|
|
|
|
|
Did you define _UNICODE in preprocessor definitions?
Maxwell Chen
|
|
|
|
|
yes the program are compiled and work as expect (I can input thai and arabic text in to database).
Only the file writing thing that didn't work.
|
|
|
|
|
You have to use binary mode to open the Unicode file.
Reference:
www.gamedev.net/community/forums/topic.asp?topic_id=362755
Maxwell Chen
|
|
|
|
|
Hi Experts,
How can I use detailed view in CListView?
|
|
|
|
|
|
You have to set the report view style, LVS_REPORT.
|
|
|
|
|
i have a class as mentioned below.The size of the class is only 16.Can anybody give me an explanation for this.Is this because of padding
class EXP
{
char c1;
char c2;
int i1;
int i2;
char *ptr;
static int mem;
};
THANKS IN ADVANCE
|
|
|
|
|
There are "paddings " between some members of a structure. This is for convenience for the CPU to access. But you can change the padding size.
To view the positions and the sizes of paddings, you can list the addresses of each members as below.
EXP obj;
printf("obj.c1: 0x%p \nobj.c2: 0x%p \nobj.i1: 0x%p \n", &obj.c1, &obj.c2, &obj.i1);
Maxwell Chen
|
|
|
|
|
|
Hi,
I am trying to open a file, whose name is in UNICODE string with a Byte-order Mark (BOM). How do I open such files? The CFile class fails to recognize such a name.
Thank you.
AJ
|
|
|
|
|
#include <fstream>
wchar_t file_name[] = L"hello.txt";
std::wfstream f(file_name, std::ios::in | std::ios::out);
Maxwell Chen
modified on Thursday, December 27, 2007 4:01:06 AM
|
|
|
|
|
Thank you for your reply. But an existing file with Byte-order Mark (Ex: FFFE), will not open with the above code.
|
|
|
|
|
Ajay L D wrote: But an existing file with Byte-order Mark (Ex: FFFE), will not open with the above code.
I just made a file with the mark EF BB BF in the beginning three bytes of the content, and the content is in Chinese. I use the below code, and it can open and load data into fstream variable. (Sorry for my typo in last reply. Note the "w" of the type name.)
std::wifstream file(file_name, std::ios::in | std::ios::binary);
12-27 05:12 pm> The content loaded is incorrect...
12-27 06:10 pm> Oh you have to use binary mode to open Unicode files.
Reference:
www.gamedev.net/community/forums/topic.asp?topic_id=362755
Maxwell Chen
modified on Thursday, December 27, 2007 5:11:48 AM
|
|
|
|
|
hello,
i am created client/server application,in server iam taking video file from the hard disk then sending to client.In client side i am reciving video data and writing into file(test.m4v) then doing decompressio,rendering using AVCODEC lib funtions.(in windows using vc++)
now i am trying without writing into file(test.m4v)directly can i take buffer then decompress it and render it.
tell me what i have to do.
thanks
AMK
|
|
|
|
|
Hi Experts,
How can I create a shell context menu for files,folders,drives and recycle bin? what is the steps I have to follow?
Thanx in advance.
|
|
|
|
|