|
delete will try to call the destructor of the object that the pointer is pointing to, and then deallocate the memory associated (by calling operator delete for that object). If you cast the type away to void, then there's no destructor associated, and as a result, all the memory will not be de-allocated properly.
I also do not understand why would you need to do such a thing.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
I use lParam of LVITEM to hold some struct address like CREATESTRUCT, PAINTSTRUCT, I new/malloc blocks of memory for them. When the listview control is about to destroy. I want to delete/free the lParam which points to a struct object. Because the object is dynamically allocated, I cannot determine its exact type. so I wish I could do the lazy job, simply delete/free the pVoid expecting that wouldn't cause too much trouble.All of windows structures don't have constructors.
|
|
|
|
|
this seems like a good place for an abstract base class with a virtual destructor and a derived class for each of the types you want to pass around.
or, you could do it with a simple struct:
struct cMyStruct
{
typedef enum {eInt=0, eDouble, eCreateStruct, ePaintStruct} eTypeID;
void *pData;
eTypeID typeID;
cleanUp()
{
switch (typeID)
{
case eInt:
{
int *p = (int *)pData;
delete p;
pData = NULL;
}
break;
etc...
}
}
};
just pass that around, initialized properly, of course.
|
|
|
|
|
Cold_Fearing_Bird wrote: Because the object is dynamically allocated, I cannot determine its exact type.
So the items in the listview "point to" different types of data? If that's the case, wrap the data and the type in a struct like:
struct data_and_type
{
int type;
PVOID data;
};
data_and_type *dt = new data_and_type;
dt->type = 0;
dt->data = (CREATESTRUCT *) new CREATESTRUCT;
SetItemData(..., (DWORD) dt); Now when you are ready to delete:
dt = (data_and_type *) GetItemData();
if (dt->type == 0)
{
CREATESTRUCT *cs = (CREATESTRUCT *) dt->data;
delete cs;
}
else if (dt->type == 1)
... Clear as mud?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
DavidCrow wrote: dt->data = (CREATESTRUCT *) new CREATESTRUCT;
Any particular reason for the explicit cast?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Any particular reason for the explicit cast?
Yes. I had not typed enough this morning and was simply trying to meet my quota.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
Cold_Fearing_Bird wrote: I new/malloc blocks
Are you sayiny you can't even tell what method you used to allocate memory? In that case you must make sure that you are calling the proper destruction method, i. e. either free() or delete (or, maybe delete [] ?). I fyou don't take care of that and use the wrong method, your program will crash at runtime.
Wherever you get the pointer from, you must add a release() method there that takes care of the proper destruction and deallocation. This most probably will involve virtual destructors in case of classes, or some other method of indirection.
|
|
|
|
|
no
does that even compile?
|
|
|
|
|
it does compiles on VS2008.
Watched code never compiles.
|
|
|
|
|
Hi all,
I have one parent dialog where in OnPaint Function i am drawing some rectangles.
On that dialog i am opening one more child dialog,
I am making that child dialog as modeless,so that i can move that dialog
I want to keep the modless dialog active even when i click on the parent dialog,so that i can move rectangles.
OnLButtonDown i need to redraw the rectangles and even keep the dialog visible.
But when i click the Mouse LButtonDown,my child dialog is getting dissappeared
void Test::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rcClient(170,414,170,415);
LeafDialog::GetClientRect(&rcClient);
if((xp<415 && xp >140) && (yp>100))
{
InvalidateRect(rcClient,false);
LeafB[0]=100;
LaefA[0] = 100;
}
}
On Test View i am creating dialog
void CTestView::OnLoadImage()
{
CBitmapimage* dlgbmp;
dlgbmp = new CBitmapimage;
dlgbmp->Create(IDD_DIALOG1,this);
dlgbmp->ShowWindow(SW_SHOW);
}
Can anyone tell me what can i do so that even i can move the image and redraw on the parent dialog when clicked on the rectangle
Thanks
Sharan
|
|
|
|
|
It sounds like the modless child dialog needs to have the HWND_TOPMOST position in the tab order so it never gets to go behind its parent window.
See the SetWindowPos function for details. You may have to be careful in its use as the modless window should not sit in top most mode when you application does not have focus.
If you vote me down, my score will only get lower
|
|
|
|
|
"UnitTest" is on the VC's main menu,I want to use itto test my C++ code, but I am not familiar with its mechanism.
I wrote a little code expecting to learn how it works.But seems not work.
here's my code
class myClass
{
private:
int m_i ;
public:
myClass( int i ):m_i(i){ }
BOOL EQ( int i )
{
if( m_i == i )
return TRUE ;
else
return FALSE ;
}
} ;
int main()
{
myClass mc( 10 ) ;
}
I click "new UnitTest", go through a few steps,when trying click the checkbox besides my project name, there's always a msg reporting unsuccessful "Unknown virtual address 0".
Waiting for help...
modified on Thursday, July 21, 2011 5:09 AM
|
|
|
|
|
I haven't used the tool myself, but isn't there a startup guide to make us understanding the procedure to be followed.
Also, you can refer the articles on MSDN and I sure you will find more than one that will guide you further.
You talk about Being HUMAN. I have it in my name
AnsHUMAN
|
|
|
|
|
Certainly good advice, but I have to say it may not be easy to find useful material: I've faced almost the same problem, and my searches turned up tons of Tools for Unit-Testing or Tutorials for using such tools, but virtually nothing about how to set up a unit test in the first place!
Maybe an explicit search at specific sites such as MSDN or various technical forums might help - I haven't tried that yet.
|
|
|
|
|
Yeah, I searched lot, still haven't found too much helpful materials.I think I should give up using it since it related tightly with netFramework.Someone says C++ test is one of the best UnitTest tool, I'll have a try.
|
|
|
|
|
Indeed. I've been spending the last hour to find something more useful, and have all but given up. At the moment I'm digesting the entries on Unit testing that I've found on Wikipedia, just to be make sure I fully understand the concepts. Maybe that's a good place for you to look too?
Unit Testing - Wikipedia
|
|
|
|
|
Thanks, Stefan. I'll take a look.I love Wiki too. It was the place that taught me how to do the matrix multiplication.Hoho
|
|
|
|
|
This might help: Unit-Testing Native C++ with Visual Studio[^]
By using the Unit Testing framework in Visual Studio, then you should automatically get the ability to see code coverage (Though it requires one of the expensive Visual Studio Editions)
|
|
|
|
|
Hi all,
i am using a droplist type combobox.i am inserted some strings in this combobox,but some long strings in combobox not fully visible.
it there any method to make visible these strings.
thanks in advance.
|
|
|
|
|
|
|
|
Hello to all,
I have written a VC++(MFC) application in which one of our USB connectivity device gets connected to computer and I continuously collect the data from that device. I display that data on screen and also when I give print command to printer through my application then that data gets printed on paper.
Now my problem is that, when I give print command to printer then it interrupts or stops data collection from the device untill the printing process gets completed but due to this we loose data from the device. So is there any way to handle printing process independently? Means can we give print command to printer using "PostMessage" or using seperate thread so that it will not stop data collection from device? or is there any other way to do this?
This is really very very urgent so please please help me out.
Thanks and Regards
Anay
|
|
|
|
|
I would suggest using threads or even a separate program to do the printing. A lot depends on where you are storing the data that you are collecting.
The best things in life are not things.
|
|
|
|
|
How to get the title text CTabCtrl's tabs?
The following code is crashing at the last line:
TCITEM tcItem;<br />
tcItem.mask = TCIF_TEXT;<br />
m_ReportTabCtrl.GetItem(ID_PANE_FOUND, &tcItem);<br />
CString csText = tcItem.pszText;<br />
Thanks & Regards
--
"Programming is an art that fights back!"
|
|
|
|