Click to See Complete Forum and Search --> : TBBUTTON's dwData & a struct - corruption?
kkez
September 26th, 2005, 02:53 PM
I'm passing a pointer to a struct to TBBUTTON struct's dwdata, but when i ask it back to display its (of the struct) contents, its members are corrupted or the program crash directly. Maybe i didn't understand dwData capacity, and i'm surely not so skilled with structs and malloc and pointers and so on...but what i'm doing wrong?
(check my example attached)
kirants
September 26th, 2005, 07:04 PM
WindowProcedure..()
{
....
A_STRUCT as;
TBBUTTON tbb[2];
ZeroMemory(tbb, sizeof(tbb));
as.c = "New";
as.b = 10;
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = 10;
tbb[0].iString = (int)"New";
tbb[0].dwData = (DWORD) &as;
as.c = "Open";
as.b = 11;
tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = 11;
tbb[1].iString = (int)"Open";
tbb[1].dwData = (DWORD) &as;
...
}
The code above has 2 problems:
the as variable is a stack variable. i.e. it is allocated when you enter the WindowProcedure function and is automatically destroyed when you return from it. That is the reason, when you retrieve it later from the toolbar, though the dwData is returned, it is pointing to an invalid location which has got freed up long time ago..
problem 2 , you are using the same structure for both items ( as ) . Result is, when you have set the iString to "Open" , inadvartently, you have changed the data for toobar item 1 also, because the toolbar 1's dwData also is pointing to the same &as location.
I hope you are understanding.. It is very important to understand one thing here. dwData is a DWORD data. That's it. For toolbar it is simply a 123456blahblah. It doesn't know if it is a pointer, a street adress or whatever. It is simply a DWORD value. How it is to be interpreted is upto the application.
In your case, DWORD is actually supposed to point to some A_STRUCT data.
What you have to do si ensure the data that you set is having enought lifetime.
You can do it in 2 ways:
Have global variables for the 2 items, as_1 and as_2 . Outside of WindowProcedure. This will give it a applications lifetime.
allocate the structs on the heap using new A_STRUCT. IN your case, you would new twice for the 2 items. In your WM_CLOSE handler, you can get it and then
delete as after getting it to free up the memory allocated.
kkez
September 27th, 2005, 11:26 AM
Thanks, i thought about the "new" solution (of course the solution of making struct global makes useless to pass the same struct as dwData :) ) but for some reason it didn't work...maybe i've passed a pointer to the pointer without thinking :)
Thanks a lot for clearing that out.
*sigh* i can't add to your reputation :( how many people must be among from my add to you and a new add?
kirants
September 27th, 2005, 12:08 PM
You are welcome .. good luck :wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.