MenuIcon problem with latest version of NPP
-
Hello again,
Franco’s original code is as follows:
void Accelerator::updateMenuItemByCommand(CommandShortcut csc) { MENUITEMINFO mii; mii.cbSize = sizeof(MENUITEMINFO); mii.fMask = MIIM_STRING; mii.dwTypeData = const_cast<LPTSTR>(csc.toMenuItemString().c_str()); ::SetMenuItemInfo(_hAccelMenu, csc.getID(), FALSE, &mii); }
Best regards.
-
Hi Yaron,
I assume you can not test it or it did not arouse your curiosity. :)
I’m curios about it but unfortunately can’t test it. :-)
This makes it very hard to see what happens under the hood.
I always need to read the documentation and afterwards do testing
in order to understand what is going on.From documentation dwTypeData
When using with the SetMenuItemInfo function, this member should contain a value whose type is specified by the fType member.
dwTypeData is used only if the MIIM_STRING flag is set in the fMask member
For me it is not 100% clear what it means, so testing would have, maybe, shed some light on it,
but without environment …
Maybe you could give it a try and addmii.fType = MFT_STRING
in addition it could be that cch member needs to be specified as well.
You see, testing …
Cheers
Claudia -
My understanding is that cch is need only for the GetMenuInfo… to tell how big is your string. It’s not needed for the SetMenuInfo since you are passing a null terminated string. Anyhow, I did try to play with it with no result.
I also tried your other hypothesis that using the fType may have an effect but I also could not get it to work.
I think the use of the SetMenuInfo is correct… I just don’t understand why the string gets mangled… and only related to the “&”… may be there is some char type conversion going on. At the end the char pointer returned by the wstring or the c_str should be theoretically identical.
-
Hello Claudia,
Thank you very much. I appreciate your help.
I’ll keep discussing it with Franco.Best regards.
-
I tried to find something which could explain the behavior but
was out of luck. In general I would expect that it works as Franco initially
wrote, const_cast removes or adds the “constness” of a variable.
Maybe you wanna check the memory window while debugging npp,
like settings a breakpoint before SetMenuItemInfo and check the location where
mii.dwTypeData refers to - maybe this gives a hint what is going on.
Or compare with ModifyMenu function likevoid Accelerator::updateMenuItemByCommand(CommandShortcut csc) { int cmdID = csc.getID(); // Ensure that the menu item checks set prior to this update remain in affect. UINT cmdFlags = GetMenuState(_hAccelMenu, cmdID, MF_BYCOMMAND); cmdFlags = MF_BYCOMMAND | ((cmdFlags&MF_CHECKED) ? MF_CHECKED : MF_UNCHECKED); LPTSTR test = const_cast<LPTSTR>(csc.toMenuItemString().c_str()); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, csc.toMenuItemString().c_str()); }
if test variable is behaving the same? Maybe give it a try with
void Accelerator::updateMenuItemByCommand(CommandShortcut csc) { int cmdID = csc.getID(); // Ensure that the menu item checks set prior to this update remain in affect. UINT cmdFlags = GetMenuState(_hAccelMenu, cmdID, MF_BYCOMMAND); cmdFlags = MF_BYCOMMAND | ((cmdFlags&MF_CHECKED) ? MF_CHECKED : MF_UNCHECKED); LPCTSTR test = csc.toMenuItemString().c_str(); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, test); }
to see if there is a problem when assigning to a new variable.
I know, nothing specific and a lot of fishing in the dark. Sorry.
Cheers
Claudia -
Hello guys,
I’m sorry if you have already tried this but the correct usage according to MENUITEMINFO specification should be
mii.fMask = MIIM_TYPE;
mii.fType = MFT_STRING;Best Regards,
Pavel -
Hello Claudia and Pavel,
Both
LPTSTR test =
andLPCTSTR test =
have the same result as Franco’s original code.mii.fMask = MIIM_TYPE; mii.fType = MFT_STRING;
I’ve tried that and some other combinations: again, the same result.
Thank you both. I appreciate your kind help.
Best regards.
-
so you are saying when assinging csc.toMenuItemString().c_str() to a variable
even ModifyMenu corrupts the menu?
Doesn’t this mean that there is something wrong with the expected result of
csc.toMenuItemString().c_str().
Franco already had the idea that there might be some kind of conversion going on
but I didn’t find it in the code (but this means nothing could be easily overlooked).But when using ModifyMenu and csc.toMenuItemString().c_str() as parameter
which is expected to be a LPCTSTR what is the difference when usingLPCTSTR test = csc.toMenuItemString().c_str(); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, test);
???
Cheers
Claudia -
Hello Claudia,
so you are saying when assinging csc.toMenuItemString().c_str() to a variable
even ModifyMenu corrupts the menu?Indeed.
But when using ModifyMenu and csc.toMenuItemString().c_str() as parameter
which is expected to be a LPCTSTR what is the difference when using…?That’s a good question. It requires some serious investigation (and investigator). :)
Thanks again.
Have a nice weekend. -
Hi again,
If the code below is the original one that is not working as expected I might have an explanation why it is not behaving well.
void Accelerator::updateMenuItemByCommand(CommandShortcut csc) { MENUITEMINFO mii; mii.cbSize = sizeof(MENUITEMINFO); mii.fMask = MIIM_STRING; mii.dwTypeData = const_cast<LPTSTR>(csc.toMenuItemString().c_str()); ::SetMenuItemInfo(_hAccelMenu, csc.getID(), FALSE, &mii); }
The problem is that when
SetMenuItemInfo
is called themii.dwTypeData
is not valid.
That’s becausemii.dwTypeData
is a pointer to aTCHAR
array - it is not the actual array and something needs to hold the actual data. Butconst_cast<LPTSTR>(csc.toMenuItemString().c_str())
is not holding anything becausetoMenuItemString()
returns temporalstd::basic_string<TCHAR>
object that is not saved in our scope and this temporal string ceases to exist the moment its pointer is stored inmii.dwTypeData
. At the momentSetMenuItemInfo
is called themii.dwTypeData
is pointing to a free memory that is on the stack and is overwritten the moment a new automatic variable is created.That’s why Claudia’s solution with local variable that is holding the new string is working -
mii.dwTypeData
is pointing to local array that is still valid whenSetMenuItemInfo
is called.BR,
Pavel -
Hello Pavel,
Thank you for the explanation. I appreciate it.
There’s still a “missing link”:
What’s the difference between::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, csc.toMenuItemString().c_str());
and
LPCTSTR test = csc.toMenuItemString().c_str(); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, test);
?
Best regards.
-
Hello Yaron,
The problem is the same.
In
LPCTSTR test = csc.toMenuItemString().c_str(); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, test);
test
is a pointer toconst TCHAR
that is pointing to the data of a temporarystd::basic_string<TCHAR>
object returned bytoMenuItemString()
. The problem is that the string object (the one actually holding the data) is temporary - it is not saved by the assignement totest
and it is destroyed immediately after the assignment.That code is practically equivalent to:
int returnInteger() { int ret = 5; return ret; } const int* pInt = returnInteger();
Here
pInt
is pointing to nonexistentint
object.Now
::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, csc.toMenuItemString().c_str());
is a function call which takes a pointer to temporary object. I’m not sure how this will behave and if it will behave consistently if the code is compiled with different compilers but that might work. That’s because here the temporary might still be alive even though it is not stored anywhere - it might still live on the stack. For sure the temporary ceases to exist on the next line (as is the case with the ‘in-between’ assignment).
As an illustration (continued from the above one) it is equivalent tovoid useIntPointer(const int* pInt) { ... } useIntPointer(returnInteger());
To be perfectly safe there’s nothing wrong with doing that:
std::basic_string<TCHAR> menuItem = csc.toMenuItemString(); ::ModifyMenu(_hAccelMenu, cmdID, cmdFlags, cmdID, menuItem.c_str());
Now the
menuItem
variable is actually holding the whole string data (it is not just a pointer) and it will be valid in our current scope.
The new compilers might not even make a copy of the string (there might be no assignment penalty) as they might simply apply move operation and directly reuse the string data returned bytoMenuItemString()
.BR,
Pavel -
Hello Pavel,
Thank you for the detailed and well built explanation.
The issue is clear.
With your permission, I’ll try to slightly sharpen it.int * returnIntPointer() { int ret = 5; return &ret; } void useIntPointer(const int* pInt) { ... } useIntPointer(returnIntPointer());
ret
is stored and valid for the duration ofuseIntPointer()
.
It should lose its validity only when we reach the end ofuseIntPointer(returnIntPointer());
; i.e. whenuseIntPointer()
ends.Is that correct?
Best regards.
-
Hello Yaron,
You are welcome.
No, the assumption is not correct.
ret
is destroyed (stops existing) the moment we exitreturnIntPointer()
:
ret
is a local variable ofreturnIntPointer()
after all.If you return it by value (as in my example) a temporary copy of
ret
is returned where the call toreturnInteger()
is. This temporary exist only in that place and if it is not immediately copied then it is lost.BR
-
Hello Pavel,
ret
is a local variable ofreturnIntPointer()
after all.I understand that.
My idea was thatreturnIntPointer()
is not destroyed until the following;
is reached.
IOW: since we useuseIntPointer(returnIntPointer());
, it’s valid until we return to);
.Otherwise, is a new temporary copy of
ret
created?
And what’s the exact definition? You can copy it just at the very beginning of the function? Isn’t it a bit far fetched?Another option is that you assume there’s not enough time to destroy
ret
because of the events proximity.
But that doesn’t make sense either.I suppose this question can be tested.
Thanks again. I appreciate your contribution to MenuIcons as well. :)
BR -
Hello Yaron,
My idea was that returnIntPointer() is not destroyed until the following ; is reached.
IOW: since we use useIntPointer(returnIntPointer());, it’s valid until we return to );That’s true for
returnInteger()
not forreturnIntPointer()
andreturnIntPointer()
is a function - it is not destroyed. The value (variable) it returns is not destroyed until the next;
.Let’s look at
returnIntPointer()
's code:int * returnIntPointer() { int ret = 5; return &ret; }
When we enter the function,
ret
is created on the stack, the stack pointer is moved (let’s say incremented for clarity).
Now we returnret
’s address and exit the function. When we exit,ret
is freed -> the stack pointer is decremented.
Now the returned pointer points to free stack memory that will hold the oldret
’s value until another local (automatic) variable is created and that usually happens when you enter another function. In our case this function isuseIntPointer()
. So inuseIntPointer()
our pointer points to invalid data.When you return
ret
by value (as inreturnInteger()
) the returned value is either in processor register or on the stack before allreturnInteger()
local variables are put there and later - destroyed. IOW there is no copy performed - it is simply a compiler matter.Your assumption is correct to the point that whatever variable a function returns (temporary) lives until the next
;
where the function is called.
InreturnIntPointer()
the returned variable isint*
and it lives until next;
after the call but it points to what? Some free memory that is overwritten perhaps and assigned to another variable.
InreturnInteger()
the returned variable isint
and it’s just what you need (it is not extra-copied on return, don’t worry, it’s just created by the compiler in the appropriate way). You can assign it to another variable to store it or directly give it to another function (copying it or using it’s address - doesn’t matter) - it is valid until the next;
after the call.Hope that clarifies.
BR
-
Hello Pavel,
The issue is clearer now.
Some of the terms you’ve used are new to me. I’ll have to expand my basic knowledge. :)Thank you for the interesting explanation. As always, I appreciate your patience.
BR
-
Hello Yaron and Pavel,
coming back from a short trip it is nice to see that you got it sorted out.
Pavel, thank you very much for your insights - very much appreciated.
Your explanation gives me the feeling that I’ve done another step in
understanding this pointer stuff, although it seems to be more like
an variable lifetime issue. :-)Yaron, thanks for asking the questions I would have asked also. :-)
Cheers
Claudia -
Hello Claudia,
Welcome back. I hope you had a good time.
The forum is not what it is when you’re away. :)Thanks again for your kind help.
Best regards.