|
Hello everyone,
I have a STL containers of pointers to objects, something like:
vector <cmyobject*> MyVector;
Suppose I want to erase an element at a specified iterator and I am not sure wheter I have to delete my object first like
delete *it;
MyVector.erase (it);
or just
MyVector.erase (it);
I suppose the first version is correct, but what confuses me is what the documentation says that erase(it) will call a destructor call.
Is this true with pointers to object as well?
Thank you very much!
Best regards,
Alexandru Savescu
|
|
|
|
|
Because it is a vector, calling erase is very expensive. If you're going to do this to a number of elements, use remove to partition them first, and if you do it a lot, consider std::list, or if you need faster lookup than std::list provides, try set.
If you have a container of pointers, then you need to delete the iterators contents first, otherwise the pointer will be deleted and not it's contents. If you have a container of objects, then the destructor is called, but calling a pointers destructor is not the same as calling the destructor of the object being pointed to. If it was, memory leaks would not be possible.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
|
|
|
|
|
Thanks Chris for your quick answer. Now everything clear.
Actually I am not using a vector, I am using a multimap My question was about deleting the pointer so the vector was the first container that came to my mind.
Best regards,
Alexandru Savescu
|
|
|
|
|
Alexandru Savescu wrote:
Thanks Chris for your quick answer. Now everything clear.
No worries. It seemed to me at first that a vector should clean up after pointers, but on reflection it's obviously not the case. I usually write a function object and use for_each to clean up my containers of pointers, so that I don't get memory leaks on shutdown, etc.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
|
|
|
|
|
My modeless dialog (the same one I was having trouble with before ) is created in it's parent (COleControl-derived) OnCreate handler:
m_msgDlg.Create(IDD_DIALOG_MSG, NULL);
When it is shown it is shown as follows:
m_msgDlg.SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
This appears at first to have the desired effect, because I now have a modeless dialog with it's own task on the taskbar.
Unfortunately, as always seems to be the way, all is not as it seems. If I left-click on it's task on the taskbar, which should minimise it, nothing happens
The dialog (which is completely custom drawn as a skinned dialog using BitBlt) handles minimising from a button click using the following code:
void CMsgDialog::OnButtonMinimise()
{
WINDOWPLACEMENT wndpl;
GetWindowPlacement(&wndpl);
wndpl.showCmd = SW_MINIMIZE;
SetWindowPlacement(&wndpl);
}
This does correctly minimise the dialog and clicking on it's minimised task in the taskbar does restore it.
Any ideas why this "standard" behaviour isn't working properly?
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
The dialog does have the WS_MINIMIZEBOX style set?
Developers that like shiny objects also dig case mods and scratch-and-sniff stickers. Klaus Probst, The Lounge
|
|
|
|
|
It wasn't set. The dialog is skinned and doesn't have a title bar, which removes access to the Minimise Box style in the resource editor.
Thanks for pointing out the obvious
Derek Lakin.
I wish I was what I thought I was when I wished I was what I am.
Salamander Software Ltd.
|
|
|
|
|
Guys,
I need to read a few registry keys on a remote server (NT/win2k) but I'm not having any luck turning up ANY documentation. I know its possible because I see that in regedit you can connect to another computer and edit its registry. Could sombody give me a pointer to an API or something in MSDN or a webpage?
thanks
Al
|
|
|
|
|
You're probably looking for RegConnectRegistry()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/regapi_803d.asp
Len
|
|
|
|
|
I have a dll which I wrote which is primarily called from C++ apps. Another group in our company wants to call the dll from VB 6.0. The function in question is coded as follows:
extern "C" __declspec(dllexport) bool _stdcall GetLoanAttributesXML(char* szxmlbuf, int nxmlbufLen)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
//return GetLoanAttributesXML(szxmlbuf,nxmlbufLen);
bool bret = false;
CString csxml = theApp.Model->getLoanAttributes();
if (csxml.GetLength() < nxmlbufLen)
{
strncpy(szxmlbuf, csxml, nxmlbufLen);
bret = true;
}
return bret;
}
The function returns LoanAttributes in xml to the caller. This call works fine when called from C++ but I get an unhandled exception when it is called from VB.
Here is the VB declaration:
Private Declare Function GetLoanAttributesXML Lib "LoanUtil.dll" (szxmlbuf As String, ByVal nxmlbufLen As Long) As Boolean
Any assistance would be appreciated!!
mantrashrim
|
|
|
|
|
Strings in VB are actually BSTR in C++. So you need to declare your functions with BSTR instead of char* and it should work (if there are no other problems )
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
Need Web-based database administrator? You already have it!
|
|
|
|
|
I have two checkboxes. The second becomes enabled when the first is checked and disabled when the first is unchecked, but I also need the second to be unchecked automatically when the first is unchecked by the user.
The following code disables the second but doesn't clear it.
***********************************************************
void CTab3_SMS::OnSaveToPCcb()
{
//Get the current values from the screen
UpdateData(TRUE);
//Is the "FIRST" checkbox checked?
if (m_bSaveToPC == TRUE)
{
//Enable the "SECOND" checkbox
GetDlgItem(IDC_DELETEFROMSIMCB)->EnableWindow(TRUE);
}
else
{
//No; clear and disable the "SECOND" checkbox
GetDlgItem(IDC_DELETEFROMSIMCB)->EnableWindow(FALSE);
}
}
***********************************************************
What other command goes into the else{} statement?
Thanks, Aoife
|
|
|
|
|
CheckDlgButton (IDC_FOO, FALSE); /ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
CheckDlgButton (IDC_FOO, FALSE);
You may want to be wary of doing things like that: while passing in FALSE works, because it evaluates to zero, same as BST_UNCHECKED , it is not really correct to do so.
Also, when newer (less experienced) developers see things like they they may be confused into thinking that TRUE (1) and FALSE (0) are not only correct, but are the only values that can be used with that function. That is, of course, an incorrect conclusion, as BST_INDETERMINATE (2) can be used as well.
Peace!
-=- James.
|
|
|
|
|
You're absolutely right. It should be CheckDlgButton (IDC_FOO, BST_UNCHECKED) .
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
The worst thing about VC7 is that it can no longer run macros and addins that were developed for VC6. Thanks, MS - really thinking about our needs here.
Does anyone know of a way around this - something the convert or tweak macros, or a way to rebuild the old add-ins?
|
|
|
|
|
Anybody knows a class for a JTAG interface, I'm doing a project and I would need this class, please,please,please.....
|
|
|
|
|
You might want to define what JTAG is in this context. Also, have you done the obvious Google search? I got 102.000 hits using just JTAG.
|
|
|
|
|
Really? I just got 46,300.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I just got 46,300.
Yup, Google says it's still about 102,000.
|
|
|
|
|
I have a splash screen in my program on startup with a 5 sec Sleep() function. Whenever I try to move the mouse around or press any key during the splash screen, the problem crashes. In the MSDN library it mentions to be carefull when using Sleep when windows are created. Any work around this.
Thanks
Anthony
|
|
|
|
|
The crash is unusual. The only danger with Sleep() is that for the duration of the Sleep() which in your case is 5 seconds, your application stops responding. But it won't crash if you move the mouse. I guess the crash is caused by something else in your code.
Nish
One little CD gone,
Then two CDs gone,
Then 5 more gone,
For a total 7 gones,
If I was a CD R,
I'd wanna cry,
Cause I'd be just a goner,
For a nasty CD burner.
[funny how frustration wakes up the poet in me]
|
|
|
|
|
That's not true at all.
The task manager reports an app as "Not responding" if the app isn't handling any messages. This is determined by task manager by using the SendMessageWithTimeout() windows api function. The timeout value task manager uses is five seconds.
Sleep() simply blocks the message pump in your app, and if your app sleeps for 5 seconds or longer, task manager reports that your app has stopped responding.
If you want to get around this, you can create a thread that implements the Sleep() api call, and then terminates itself. Then, you can WaitForSingleObject() on the thread before continuing processing.
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
Please review the Legal Disclaimer in my bio.
|
|
|
|
|
I think you and Nish just said the same thing. It is just he was looking at it from a single threaded window application. In that case sleep would make it seem like the application had locked up.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
I'm curious - why are you using Sleep() ? If you want to keep the splash screen alive for 5 seconds, use a timer instead. When the timer fires, close the splash screen window.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|