|
I am using a vector to hold a list of pointers that are newed in the start element handler of the Expat parser. Everything works fine until I exit the application and run my cleanup function.
The following code appears to fail or at least produces a memory leak.
Button *pButton; // pure virtual
vector<button*> vButtons; // holds list of created buttons
// from start handler
pButton = new Label( c_Label );
vButtons.push_back( pButton );
// Cleanup
if( count > 0 )
{
vector<button*>::iterator itClean;
for (itClean = vButtons.begin(); itClean != vButtons.end(); ++itClean)
{
delete *itClean; // crashes here everytime
}
vButtons.clear();
}
Any idea why this is leaking?
-Steve
|
|
|
|
|
What you're doing looks absolutely correct to me. What error do you get when it crashes ?
BTW, this would be easier to read if you checked 'Do not treat <'s as HTML tags' or typed < for <
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Sorry about the formating, I'll remember next time.
I am not sure whats happening. The destructor for Label is called and then goes into the following
code. I am just getting user break point called and it seems to crash here:
"Label::`scalar deleting destructor'(unsigned int 1) + 37 bytes"
What is a scalar destructor?
004028B5 mov eax,dword ptr [ebp+8]
004028B8 and eax,1
004028BB test eax,eax
004028BD je Label::`scalar deleting destructor'+3Bh (004028cb)
004028BF mov ecx,dword ptr [ebp-4]
004028C2 push ecx
004028C3 call operator delete (0041aec0) // crashes here
004028C8 add esp,4
004028CB mov eax,dword ptr [ebp-4]
004028CE pop edi
004028CF pop esi
004028D0 pop ebx
004028D1 add esp,44h
004028D4 cmp ebp,esp
004028D6 call _chkesp (0041b29e)
004028DB mov esp,ebp
004028DD pop ebp
Sorry, I can't tell you much more that this so far.
-Steve
|
|
|
|
|
smesser wrote:
What is a scalar destructor?
I'd be looking on the object you're deleting to see if it has it's own method you call instead to clean it up. Then I'd try doing nothing and see if memory still leaked ( although it certainly should ).
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Christian,
I have tryed these sugguestions already. I can't image what is happening. This code used to work.
When it worked I was creating all my buttons in OnInitDialog() and then upon exiting I call
void CHTPCCOREDlg::Cleanup()
{
if ( m_pMemGraphics != NULL )
{
delete m_pMemGraphics;
m_pMemGraphics = NULL;
}
if ( m_pMemBitmap != NULL )
{
::delete m_pMemBitmap;
m_pMemBitmap = NULL;
}
if ( m_pCachedBitmap != NULL)
{
::delete m_pCachedBitmap;
m_pCachedBitmap = NULL;
}
KillTimer( UPDATETIME );
KillTimer( CHECKKEYS );
KillTimer( MOUSEMOVE );
int count = vButtons.size();
if( count > 0 )
{
std::vector<Button*>::iterator itClean;
for (itClean = vButtons.begin(); itClean != vButtons.end(); ++itClean)
{
delete *itClean;
}
vButtons.clear();
}
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
It was all working fine and then I decided to use XML to allow the buttons to be user definable.
In stead of creating my buttons in OnInitDialog() I am now creating them in the start handler while parsing the configuration file. That should be the only difference and now the code leaks.
Any more suggestions?
-Steve
|
|
|
|
|
Christian,
After some further investigation I am getting this:
memory check error at 0x003267A9 = 0x00, should be 0xFD.
memory check error at 0x003267AA = 0xF0, should be 0xFD.
memory check error at 0x003267AB = 0x41, should be 0xFD.
First-chance exception in HTPCCORE.exe: 0xC0000005: Access Violation.
I have never seen memory errors like this before.
I am wondering if expat is calling its handler functions in another thread and that is the problem. I uncommented my old code and it works perfectly but as soon as I put back in the new code I get the above errors. By the way I did find a couple of other small memory leaks that my new code caused and I am down to the errors mentioned above.
-Steve
|
|
|
|
|
These errors probably indicate you're walking over memory you don't own - I believe debug mode fills a buffer with FD either side of any memory you allocate, so it can tell when you trounce it.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
If that is the case I can't imagine how since the only difference in the code is where the memory is allocated. This is a strange one.
Thanks for your input.
|
|
|
|
|
This you proably took care of, but is the dtor of Button declared as virtual? Anyway, if it wasn't, the problem would show also with your old code, so I'd look next to the different thread hypothesis.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquin,
It ended up being something really strange.
In my class definition I was adding variables as needed.
int ButtonNumber;
CString TitleCaption, SubCaption, DllName,CommandName, Type;
CString FontName, FontStyle, Purpose;
REAL X,Y,W,H;
float TextPad,ImagePad;
Region *region;
Color *BaseColor, *DefaultColor, *FillColor;;
Image *image;
UINT Width, Height;
REAL FontSize;
I don't know why it worked in the old code but in the new code the addition of "REAL FontSize" was causing the crash. I added FontSize to "REAL X,Y,W,H,FontSize" and removed it from the end of the list of variables and it worked correctly.
It seems that the compiler was having problems lining up the memory for the variables if I didn't keep like variables together.
-Steve
|
|
|
|
|
Ummm... this smells very bad. I don't think the compiler is doing a mess with members of your class, most likely you're just masking the error.
Maybe you can try the following: add a trace message in your startHandler outputting the sizeof of the class, and compare with what you've got in the main program. Maybe this sheds some light on the problem.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Very good sugguestion. Yes, I know very suspicious behavior.
// crashes
CString Title, Alignment, m_Type;
CString FontName, FontStyle;
int ButtonNumber;
int Padding;
REAL X,Y,W,H;
Region *region;
Color *FontColor;
Color *FillColor;
bool bFill;
REAL FontSize;
// Works fine
CString Title, Alignment, m_Type;
CString FontName, FontStyle;
int ButtonNumber;
int Padding;
REAL X,Y,W,H;
REAL FontSize;
Region *region;
Color *FontColor;
Color *FillColor;
bool bFill;
In all cases and places the size of label is the same (65).
Any other ideas?
|
|
|
|
|
Try using a list instead of a vector to holder the pointers.
|
|
|
|
|
I could try this do you have any reason or explaination to why this would make a difference?
Thanks,
-Steve
|
|
|
|
|
I know why you're crashing!! It happened to me too, and before I discovered why, it was driving me crazy.
Here's the reason (seriously)!! You're trying to delete pointers to memory that have already been deleted.
I discovered that the compiler will call its own destructor on the vector, which will automatically do the cleaning up for you. But later, when you try doing the cleaning up yourself, you are in effect deleting memory that has already been deleted, and that's what causing the crash. IOW, DON'T do the deleting yourself. Just don't do it. The compiler has already done it for you. (I'm talking from experience. It happened to me also.)
William
Fortes in fide et opere!
|
|
|
|
|
William,
Thanks for the insight urr ( experience from the school of hard knocks ).
-Steve
|
|
|
|
|
Hi All,
I was wondering if anyone has done any development in WTL with the CCheckListBox? I have a ListBox but want to give the functionality (and feedback to user) that they have selected multiple items.
I have tried using the macro's described in MSDN but have had no luck.
ListView_SetExtendedListViewStyle(this->m_ipCheckList.m_hWnd, LVS_EX_CHECKBOXES);
When I mean no luck, I mean that the above call has no effect on my ListBox (no check box appears next to it).
Any thoughts on what I would have to do to get something like the above going?
thanks
Bryce
|
|
|
|
|
A listbox and listview
are two different controls. Your code will work for a list view
as long as its in report mode but will not for a listbox.
This works in some code I'm currently using for a ListView.
ClistViewControl m_listView
m_listView.Attach( GetDlgItem( IDC_LIST_STATION_DATA) );
m_listView.SetExtendedListViewStyle( LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
Correct me if I am wrong but the CCheckListBox is a mfc construct not
wtl. The following url explains how to use it
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cchecklistbox.asp
Hope this helps
|
|
|
|
|
Hi,
Thanks for your reply.
I thought there may have been another version of the CheckListBox for WTL... oh well...
How have you defined what your ClistViewControl object is?
The supplied code will not compile as there is no definition of ClistViewControl.
I tried to convert it to CListViewControlImpl m_ListView;
with below in stdafx.h
<br />
class CListViewControlImpl : public CWindowImpl<CListViewControlImpl, ClistViewControl><br />
{ DECLARE_EMPTY_MSG_MAP(); };<br />
Can you please supply your definition on your ClistViewControl.
Thanks
Bryce
|
|
|
|
|
Hi, Im using WTL version 7. In order to use the control wrapper
just add
#include <atlctrls.h> to your stdafx.h.
then you can declare CListViewCtrl m_listView;
and use it accordingly
If your still having problems i can send you a sample app.
Hope this helps
edit:
whoops the <> were lost in html.
|
|
|
|
|
Thanks for your help.
I can get it to compile now!
I did change it a little bit so that DDX can work.
in stdafx.h define like below
<br />
class CListViewCtrlImpl : public CWindowImpl<CListViewCtrlImpl, CListViewCtrl><br />
{ DECLARE_EMPTY_MSG_MAP(); };<br />
then in your class you use CListViewCtrlImpl instead of CListViewCtrl.
What method did you use to add elements to the List?
I simple demo app would be handy. please send thanks
cheers
Bryce
|
|
|
|
|
I sent you a sample app with adding of elements. Let
me know if this is what you needed.
Thanks,
Clay
|
|
|
|
|
A list box and list view control are different things. The list view has checkbox support built-in. If you want checks in a list box, you'll need to write it yourself (CCheckListBox is an MFC class, I suppose you could take that code and port it to WTL).
WTL has CCheckListViewCtrl that wraps a list view with checkboxes. Look in atlctrlx.h for its definition.
--Mike--
Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber
There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.
|
|
|
|
|
hi
please guide me in my problem.
when I use then generated code in VC6 in a WTL program,
Paint event is like this:
<br />
OnPaint(UINT , WPARAM , LPARAM , BOOL& )<br />
{<br />
CPaintDC dc(m_hWnd);<br />
<br />
return 0;<br />
}<br />
but when i use dc. IDE dosen't show me AutoComplation Pop-up.
I need to know the reason.
oh, i know some ways to solve:
we can use something like this
CPaintDC dc=CPaintDC(m_hWnd);
but why the IDE have troble with first one?
|
|
|
|
|
Hi,
I'm novice in STL C++ librari, so my question will be very easy.
I have a structure, for example:
typedef struct _mystruct
{
char name[256];
bool sex;
int age;
} mystruct, *pmystruct;
I can define a class based on queue STL template:
typedef queue<mystruct, list<mystruct=""> > MEMBERS_QUEUE; // This works perfectly
But I want also define a class based on deque:
typedef deque<mystruct, list<mystruct=""> > MEMBERS_DEQUE; // This doesn't work.
I suspect that I must define allocator for mystruct... What should I do to construct own class based on deque and mystruct?
Yours sincerely,
Alex Bash
|
|
|
|