|
My colleague wrote the following code and I wander why there are no "delete" statement for the two string variables *pSNew and *pSN.
Is there a memory leak?
Urgent! Thanks
void CCommTSP32View::OnPMDeviceFound( long _id, long _device )
{
// now it critical!
EnterCriticalSection( &m_csCommunicationSync ) ;
string *pSNew ;
string *pSN ;
// Init
pSNew = new string ;
pSN = (string*)_device ;
*pSNew = *pSN ;
PostMessage( WM_SPY_DEVICE_FOUND, _id, (long)pSNew ) ;
// release critical section
LeaveCriticalSection( &m_csCommunicationSync ) ;
}
ccp
|
|
|
|
|
string *pSNew should really be deleted after the call to PostMessage, but since the pointer is being passed to another part of the code, it's possible it is being deleted there. Take a look at the WM_SPY_DEVICE_FOUND handler to verify what is happening to the lParam.
Generaly, it's bad practice to allocate and deallocate memory in seperate parts of the code. As a rule, any time I delete something, I will set it to NULL, then, at the same place of allocation, I will try to delete again if no longer required.
|
|
|
|
|
why do u need to set it to Null before delete?
|
|
|
|
|
ccp999 wrote: why do u need to set it to Null before delete?
Actually, this is nonsense.
By setting a pointer to NULL you are invalidating it, not deleting the memory it points to.
Its the other way round: delete it, then set it to NULL (so that no one would use that pointer which does no longer point to memory you own again).
delete on a NULL-pointer does no harm, though.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
|
|
|
|
|
What is the purpose of this code?
pSNew = new string ;
pSN = (string*)_device ;
*pSNew = *pSN ;
why can't just use _device directly?
|
|
|
|
|
Without seeing the whole code it's hard to say, but what it is doing is making a copy of the variable.
You have to set the pointer to null otherwise delete will throw an exception.
|
|
|
|
|
I'm afraid that you can not delete the string because the PostMessage method posts the message and immediately returns. Note SendMessage sends the message and waits until it is processed. So if you delete the string the method which will process the message (sent via PostMessage) will probably crash because the pointer is invalid. Therefor the message processing should delete the string.
Moreover if this message is posted to different application it is even complicated. In that case I recommend to use GlobalAddAtom and GlobalDeleteAtom. But probably this is not your case...
|
|
|
|
|
Let's see how would I say this I have never really used visual C++ for anything. If any one could start me off in the right direction with a good tutorial that would be nice. Or at least tell me some of the abbreviations for certain things. Thanks.
I'm a true failure.
|
|
|
|
|
|
|
it is a nice site.
http://www.functionx.com/visualc
|
|
|
|
|
Is it possible to use the tab order set for edit controls in the dialog editor in the program? Is it possible to access an edit box via its tab order?
I can access by the control ID like this:
CWnd* pt;
pt = AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_EDIT1); <-----uses IDC_EDIT1
pt->SetWindowText("AAAAA");
Or I can access a different edit box by an offset to the first ID Like this:
pt = AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_EDIT1 + 8); <----uses IDC_EDIT1 + 8
Or I can set up the IDs in an array, and access like this:
pt = AfxGetApp()->m_pMainWnd->GetDlgItem(IDArray[5]); <----uses ID stored in array
So, this leads me to ask if there is some way to use the tab order defined in the dialog editor to do something similar.
Thanks
|
|
|
|
|
You can iterate through the controls in their tab order with GetNextDlgTabItem()
|
|
|
|
|
The tab order determines what order the controls are placed on the dialog window
by changing the order of the control statements in the dialog resource.
I suppose you could enumerate all the child windows of the dialog with GetTopWindow() and
GetNextWindow() (which would iterate through the controls in reverse tab order) or use
EnumChildWindows(). For each child window, check its class with GetClassName() to see if it's an
edit control. If it's an edit control then process it accodingly.
This seems like a lot of work considering you have full control over which controls are in
the dialog and what all of their IDs are, even if you add or remove controls at runtime. Unless
controls are being added/removed by another process...
If I knew what you are trying to do I could maybe give you a more specific code example
Mark
|
|
|
|
|
Hi all, I was trying to change the application icon through the windows class by changing the icon macros loaded into windows class as shown below, but no matter what icon macro I choose the icon displayed is the application icon from IDI_APPLICATION macro.
<br />
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
...<br />
...<br />
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);<br />
Could please tell how to change the icons by icon macros. Any help would be greatly appriciated.
Thanks
Scody
|
|
|
|
|
if you run this code wcex.hIconSm= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); what happens?
From the MSDN
HICON LoadIcon(HINSTANCE hInstance,LPCTSTR lpIconName);
hInstance
[in] Handle to an instance of the module whose executable file contains the icon to be loaded. This parameter must be NULL when a standard icon is being loaded
|
|
|
|
|
Hi WhiteSky,
I have just tried the code as you suggested
<br />
windowClass.cbSize = sizeof(WNDCLASSEX);<br />
windowClass.style = CS_HREDRAW | CS_VREDRAW;<br />
windowClass.lpfnWndProc = WndProc;<br />
windowClass.cbClsExtra = 0;<br />
windowClass.cbWndExtra = 0;<br />
windowClass.hInstance = hInstance;<br />
windowClass.hIcon = LoadIcon(windowClass.hInstance, (LPCTSTR)IDI_WINLOGO);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "AeroClass";<br />
windowClass.hIconSm = LoadIcon(windowClass.hInstance, (LPCTSTR)IDI_WINLOGO);<br />
but still the default application icon is displayed. I would like to use standard icon Windows logo to be displayed on my application window, so I was using NULL in my code in the previous posting.
All I want to do is change the icon on my application window to Windows logo using standard icon macro IDI_WINLOGO.
Any help would be greatly appriciated.
Thanks
Scody
|
|
|
|
|
open the resource header file, find the definition for the icons. Give the icon you want the smaller number. The default Icon for an exe is the first icon found (the one with the smallest number).
|
|
|
|
|
You can override PreCreateWindow() to use your custom window class instead of the default...
This example assumes szMyRegisteredWindowClass points to the class name associated with your
"windowClass"
BOOL MyWindowClass::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CBaseWndClass::PreCreateWindow(cs) )
return FALSE;
cs.lpszClass = szMyRegisteredWindowClass;
cs.style = <possible override default window style here>;
cs.dwExStyle = <possible override default extended window style here>;
return TRUE;
}
|
|
|
|
|
Hello,
I want to send mails(with attachments) from my MDI based application.
I got lot of articles from CodeProject regarding MAPI. I choose "David Brooks" article which uses simple MAPI.
Here is my code ...
class CSendFileTo
{
protected:
HWND m_hWndParnt; // Window handle
enum Error {SUCCESS, NO_ATTACHMENT=0x1001, NOT_A_WINDOW, LIB_LOAD_FAILED, GETPROC_FAILED};
// MAPI related data members
HINSTANCE hMAPI;
public:
CSendFileTo();
virtual ~CSendFileTo();
CSendFileTo(HWND hWnd)
{
m_hWndParnt = hWnd;
}
unsigned int SendMail(CString const &strAttachmentFileName,CString const &strSubject=_T(""),CString const &strToAddr=_T(""),CString const &strToName=_T(""),CString const &strBody=_T(""))
{
if (strAttachmentFileName.IsEmpty())
return NO_ATTACHMENT;
if (!m_hWndParnt || !::IsWindow(m_hWndParnt))
return NOT_A_WINDOW;
hMAPI = ::LoadLibraryA(_T("MAPI32.DLL"));
if (!hMAPI)
return LIB_LOAD_FAILED;
ULONG (PASCAL *SendMail)(ULONG, HWND, MapiMessage*, FLAGS, ULONG);
(FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));
if (!SendMail)
{
::FreeLibrary(hMAPI);
return GETPROC_FAILED;
}
TCHAR szFileName[_MAX_PATH];
TCHAR szPath[_MAX_PATH];
TCHAR szSubject[_MAX_PATH];
TCHAR szBody[_MAX_PATH];
TCHAR szToName[_MAX_PATH];
TCHAR szToAddr[_MAX_PATH];
strcpy(szFileName, (LPCTSTR)strAttachmentFileName);
strcpy(szPath, (LPCTSTR)strAttachmentFileName);
strcpy(szSubject, (LPCTSTR)strSubject);
strcpy(szBody, (LPCTSTR)strBody);
strcpy(szToAddr, (LPCTSTR)strToAddr);
strcpy(szToName, (LPCTSTR)strToName);
// Setting Attachment Info
MapiFileDesc fileDesc;
ZeroMemory(&fileDesc, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;
fileDesc.lpszPathName = szPath;
fileDesc.lpszFileName = PathFindFileName(szFileName); // Remove the path
// Setting Recepient Info
MapiRecipDesc recpnts;
ZeroMemory(&recpnts, sizeof(recpnts));
recpnts.ulReserved =0;
recpnts.ulRecipClass = 1;
recpnts.lpszName = szToName;
recpnts.lpszAddress = szToAddr;
//Setting the Message Info
MapiMessage message;
ZeroMemory(&message, sizeof(message));
message.lpszSubject = szSubject;
message.nFileCount = 1;
message.nRecipCount = 1;
message.lpszNoteText = szBody;
message.lpRecips = &recpnts;
message.lpFiles = &fileDesc;
int nError = SendMail(0,m_hWndParnt, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
return nError;
::FreeLibrary(hMAPI); // Freeing the DLL handle
return SUCCESS;
}
void ShowError(UINT errCod)
{
if (errCod)
{
CString str;
str.Format("Error: %X", errCod);
AfxMessageBox(str);
}
}
};
// Invoking Send Mail function
void CDefectLinkerDlg::OnButtonSend()
{
CSendFileTo sendTo(this->m_hWnd);
int nError = sendTo.SendMail(m_attachments,strSubject,strURL,strName,strBody);
if(nError != 0)
{
sendTo.ShowError(nError);
}
}
I can run this application. I got the MS Outlook 2000 window and it shows my mail with attachment.
But when I press the "Send" button in Outlook , both my application and the MS
Outlook program is closed.
But that mail is located in my Outbox. But Outlook cannot deliver that meesage ..,I got "Mail delivery failure" error...!
What will be the problem ?
Pls help me...
Thanks in advance
vinsankar
|
|
|
|
|
What return value of sendTo.SendMail
|
|
|
|
|
Hello,
SendMail returns '0' (i.e Sucess)
But the application that calls MAPI closed when I press the "Send" button.
Thanks,
vinsankar
|
|
|
|
|
But when I run your code its working and if click on Send button in Outlook its working and also when I close Outlook it returns to my program I think maybe your problem isnt of this code
|
|
|
|
|
Did your problem solve?
|
|
|
|
|
Hello,
No ...But I overcome that problem by using MS Outlook automation...
I Don't know ...why that Simple MAPI program crashes when using with an MDI app...?
Thanks...& regards...
vinsankar
|
|
|
|