Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crash on Relese not Debug 2

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
I created a stock MFC CView. The only fucntion I added is as follows:

Code:
void CFtp4View::OnConnect()
{
	CInternetSession MySession;
	CString string;
	AfxGetMainWnd()->SetWindowText("Transfering");
	try
	{
		CFtpConnection * MyFtpConnection = MySession.GetFtpConnection("[URL unfurl="true"]www.dynacomproducts.com",[/URL] "dynaco", "dynacom51");
		BOOL result = MyFtpConnection->PutFile("C:\\Dynacom\\Dynapro.mdb", "Dynapro.mdb");
		
		
		
		LPVOID lpMsgBuf;
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0,(LPTSTR) &lpMsgBuf,0,NULL);

		string = (LPCTSTR)lpMsgBuf;

		
		if (result)
		{
			
			c_Text.SetWindowText(string);
			string = "File Placed";
		}
		else			
			c_Text.SetWindowText(string);

		
		WINDOWPLACEMENT WindowInfo;
		AfxGetMainWnd()->GetWindowPlacement(&WindowInfo);
		WindowInfo.showCmd = SW_SHOWNORMAL;
		AfxGetMainWnd()->SetWindowPlacement(&WindowInfo);
		AfxGetMainWnd()->FlashWindow(TRUE);
		GetParentFrame()->BringWindowToTop();
			
		AfxGetMainWnd()->SetWindowText("Completed");
		MessageBox(string);

		

		
	}
	catch (CInternetException* pEx)
	{
		TCHAR sz[1024];
      pEx->GetErrorMessage(sz, 1024);
	  MessageBox(sz);
	  c_Text.SetWindowText(sz);
	}

}

The program crashes when I "x" out to close, on relese only. It runs fine in debug.

Any Ideas?
 
Usully this happens when you do not deallocate sone pointers on the heap. In the debug it runs fine becaus somehow the debug code has some lines that meakes the delete.

I don't know exactly but I think you must try to delete all your heap pointers. Or look for methods like 'Delete...' inside of the pointed object, and call them on end.

Maybe there is a VS bug, install your latest SP. (5 I suppose).

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
I did some checking, and it seems that this line is causing me problems

ON_MESSAGE(WM_MYCONNECT, OnConnect)

as part of the bigger function

BEGIN_MESSAGE_MAP(CFtp4View, CFormView)
//{{AFX_MSG_MAP(CFtp4View)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MYCONNECT, OnConnect)
END_MESSAGE_MAP()

where I define WM_MYCONNECT as

#define WM_MYCONNECT WM_USER + 5

Any clues?

Or, as a work around, I want OnConnect to run after the initial view loads up. I did this by putting PostMessage(WM_MYCONNECT) at the end of the views OnInitialUpdate()

Can you think of any other way I can get this function OnConnect to run after the view load and without any user input?

Thanks for your time,

CJB
 
Your OnConnect function is receiving 2 parameters like lparam and wparam. like any message function should.

This is one idee that came into my mind...

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
1. You should write:

if(lpMsgBuf) {
string = (LPCTSTR)lpMsgBuf;
LocalFree(lpMsgBuf); //Free Memory from FormatMessage()
}
2. What kind of Window is c_Text? Where is it created and deleted? Is it created before Cftp4View and deleted after them?
3. Your OnConnect() should have Parameters from Message (like standard Function OnCreate( LPCREATESTRUCT lpCreateStruct ); etc...)
4. If You wish to show somewhat in a Window, do it (call it from) Function OnShowWindow(...) (to override it, use Class assistant for WM_SHOWWINDOW).
This Function is called after a Window it ready and initialized.
5. I think, You wish to use Your connection not only in OnConnect(), but Your Object - MySession and Pointer MyftpConnection exists in OnConnect() only. How are they created and destroyed (without memory leaks?). If You wish to work with them, make they to Members of Cftp4View (or are they not Members in this sample code only?).
6. GetParentFrame()->BringWindowToTop(); Is Parent Frame ready to show before You You call OnConnect()?
7. You should use AfxMessageBox(string); instead MessageBox(string);, if You use another Afx - Functions (it is not Error and can't cause any leaks).
8. You should write:
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1023);
(one char for the 0 on the ehd of string).
9. You should delete CInternetException* pEx in Your catch{} - statement.
10. Sometimes CString - Objects in Wnd- Objects are not clearly deleted - if You find out with Debugger, that it is so (You can see Addresses of leaks and compare they with Objects in Your Functions), You can use:
CString * string = new CString((LPCTSTR)lpMsgBuf);
...
delete string; //After You do not need it more
I hope, it can help You. It all are not errors, but some fmom them can cause memory leaks.
 
Thank you both very much. My problem was the incorect message function. I re-wrote it to accept lparam and wparam and return LRESULT and it works great.

tchouch to "touch" on a couple of your points.

if(lpMsgBuf) {
string = (LPCTSTR)lpMsgBuf;
LocalFree(lpMsgBuf); //Free Memory from FormatMessage()
Why do I need to Free that memory? I thought it would be freed automatiocaly when I leave OnConnect? I create the variable in OnConnect, so I thought it would be freed when I leave.

4. If You wish to show somewhat in a Window, do it (call it from) Function OnShowWindow(...) (to override it, use Class assistant for WM_SHOWWINDOW).
[red] This didn't do what I was looking for. This event is fired when a window is about to be show, so it fires before I can see my original window. I was looking for something that fires right after I can see my original view[/red]

5. I think, You wish to use Your connection not only in OnConnect(), but Your Object - MySession and Pointer MyftpConnection exists in OnConnect() only. How are they created and destroyed (without memory leaks?). If You wish to work with them, make they to Members of Cftp4View (or are they not Members in this sample code only?).
[red]actually I use them only in OnConnect. I added this code later to the end of my OnConnect function:
if (MyFtpConnection != NULL)
MyFtpConnection->Close();
delete MyFtpConnection;[/red]

6. GetParentFrame()->BringWindowToTop(); Is Parent Frame ready to show before You You call OnConnect()?
[red]your right, this does not work. I was trying to bring the window from the task bar and restore it once the function was done. I decided to just change the task bar text to "completed" and flash the window.[/red]

7. You should use AfxMessageBox(string); instead MessageBox(string);, if You use another Afx - Functions (it is not Error and can't cause any leaks).
[red] Could you explain why one is better, or link me to a resource so I can read it? I always use MessageBox. If Im doing it wrong, I would sure like to know.[/red]

8. You should write:
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1023);
(one char for the 0 on the ehd of string).
[red] actually according to msdn, the second paramater for GetErrorMessage is

nMaxError
The maximum number of characters the buffer can hold, including the NULL terminator.

so I should be able to pass 1024.[/red]

9. You should delete CInternetException* pEx in Your catch{} - statement
[red] thanks for the tip, I added the code[/red]

10. Sometimes CString - Objects in Wnd- Objects are not clearly deleted - if You find out with Debugger, that it is so (You can see Addresses of leaks and compare they with Objects in Your Functions), You can use:
CString * string = new CString((LPCTSTR)lpMsgBuf);
...
delete string; //After You do not need it more
[red] can you explain to me how I can see addresses of leaks, or send me a link that discusses it, as I'm sure there are many papers on this out there[/red]


Again, thank you guys so much. Not only did you debug my program for me, I picked up some very valuble tips on Windows framwork and memory managment.


 
1. About Memory in FormatMessage(): I don't know why Microsoft does it, but it is so, if You use this Function with FORMAT_MESSAGE_ALLOCATE_BUFFER. I think, compiler can't analize, what You do with lpMsgBuf.
5. I hope, You use such code:
if (MyFtpConnection != NULL) {
MyFtpConnection->Close();
delete MyFtpConnection;
}
If You try to delete a Object with NULL pointer, it can cause problems.
7. Afx - Functions are better (Microsoft says it), if You use multiple threads (and You do it, I think, at least with GetftpConnection()). Info You can find on MSDN (8. You are right, but I use allways one byte more: it is not expensive, and I can't think allways about details. Some Functions, for example GetWindowText(), do not include NULL char, some include, and all Functions get a length of strings without NULL char. So I do as a Standard, for example:
void someFunction(CWnd *pWnd) {
if(pWnd == NULL)
return;
if(!IsWindow(pWnd->m_hWnd)) //If pWnd == NULL, program exits on this place
return;
...
//I do it normally with Message WM_GETTEXTLENGTH
int nMaxCount =pWnd->GetWindowTextLength( );

LPTSTR lpszStringBuf = new char[ nMaxCount +1]; //If nMaxCount = 0, it works too
if( lpszStringBuf)
//I do it normally with Message WM_GETTEXT
pWnd->GetWindowText(lpszStringBuf , nMaxCount );
else {
AfxMessageBox("Not enough Memory!");
return;
}
...
delete [] lpszStringBuf;
...
}

10. You can see addresses and sizes of memory leaks (if You have any) after Your Program has exited in Debug mode. You must save this info and repeate Debugging with checking of addresses and sizes of all objects and pointers of memory arrays, they will be normally the same (attention, some local variables can have the same addresses - check sizes of them). It needs some work, of course. A lot of Functions allocate memory and You should free it after usage (CreateBitmap() etc.). And if You allocate some Memory with any commands, You should free it after You do not need it more (new - delete, GlobalAlloc-GlobalFree, LocalAlloc-LocalFree, malloc-free, HeapAlloc-HeapFree, HeapCreate-HeapDestroy etc.). You must allways think about it if You creates a program.

Good lake.
 
Thanks again tchouch, I'll read up on Afx functions, and I understand what your saying about always adding 1 more byte. If you get into the habit it will save you the time of looking up each function, and certainly save you the time dealing with intermitend array overruns.

CJB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top