Share via

AppendMenu function gving memory exception

Pooja Kamra 11 Reputation points
2022-02-17T08:30:22.613+00:00

We are creating popup menu in one of our MFC components. To create popup menu dynamically we are using CreatePopupMenu and then adding items using AppendMenu and then using TrackPopupMenu.PFB code snippet:
m_popupMenu.CreatePopupMenu();
LPTSTR data1 = (LPTSTR)malloc(20);
DWORD data = PALETTERGB(255, 255, 255);
_ultot(data, data1, 10);
m_popupMenu.AppendMenu(MF_ENABLED | MF_OWNERDRAW | MF_MENUBREAK,ID_COLOR_WHITE,data1);

m_popupMenu.AppendMenu(MF_ENABLED | MF_OWNERDRAW,ID_COLOR_BLACK);
m_popupMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
PointX, PointY, this);
Here we are passing addition information in first menu item. Whenever we are passing it, we are getting exception as below:

"Exception thrown at 0x7476C3CA (clr.dll) in DrawAndSaveZone.exe: 0xC0000005: Access violation reading location 0x00360031.
If there is a handler for this exception, the program may be safely continued."

If we are not using third parameter, everything is working fine,

Please help to resolve it

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.