|
Hi All,
I have some Spin control - and i want to increment or decrement the value in my edit box in 0.1 in each spin step.
I try to look in the MSDN and i see that each increment or decrement of the a value is in integer value step.
How can i increment or decrement in 0.1 in each step ?
( I mean that if i have in the Edit box the value 5 and i press on increment arrow that i need to have the value 5.1 in the edit box )
Thanks for the help.
|
|
|
|
|
Yanshof wrote: I try to look in the MSDN and i see that each increment or decrement of the a value is in integer value step.
this is a workaround not a actual programming!, you just have to a variable which contain the current value in edit control and you have to handle the EN_CHANGE message of edit control. i.e. when ever the value of edit box change due to spinctrl, you update the value in edit control accordingly!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
Hello everyone,
In GotW #66, one of the moral is the exception handler of constructor should not do any like resource free task. I do not agree. Here is the quoated moral and my code to prove this moral will have memory leak.
Anything wrong with my analysis?
http://www.gotw.ca/gotw/066.htm
Moral #1: Constructor function-try-block handlers have only one purpose -- to translate an exception. (And maybe to do logging or some other side effects.) They are not useful for any other purpose.
class A
{
private:
int* p;
public:
A()
try
{
p = new int[10];
}
catch (bad_alloc)
{
}
catch (...)
{
if (p) delete[] p;
}
}
thanks in advance,
George
|
|
|
|
|
He also continues to say that
- resources should be allocated in the body (rather than the initializer list)
- failed allocation of these resources should be handled by a "normal", not a function exception block.
The point is that for the scenario discussed - base class or member constructors failing - THERE IS NO INSTANCE OF A, and following from that there is no p that you could test.
|
|
|
|
|
Thanks peterchen,
I agree that there is no instance of A. But there exists memory allocated and pointed by pointer p, right? We need to free it if it is allocated successfully (in my case, there is no bad_alloc exception, which indicates the memory is allocated and we must free -- there is some other exception afterwards)
p = new int[10];
regards,
George
|
|
|
|
|
No, becaue the exception you are catching is thrown before that allocation is executed.
|
|
|
|
|
Sorry peterchen,
I may not make myself fully understood. I mean we allocate memory on heap successfully, using
int* p = new int[10];
then there is another exception thrown after we successfully allocate the memory. In this case, it is leaked, right (so RAII model does not work in this case)?
regards,
George
|
|
|
|
|
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
|
|
|
|
|