What's new

Weird Memory Problem

Doomulation

?????????????????????????
I'm at a loss; there seems to be some BIG error, but I do not know what.
Look at this simple declaration (MFC):

Code:
// CCommandLauncherDlg dialog
class CCommandLauncherDlg : public CDialog
{
// Construction
public:
	CCommandLauncherDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_COMMANDLAUNCHER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
protected:
	virtual void OnCancel();
	virtual void OnOK();
public:
	afx_msg void OnClose();
	CString m_strCommand;
	CString m_strCommand2;
	afx_msg void OnBnClickedOk();
};

When creating this dialog and then destructing it, wether it is on the heap or the stack, the CString objects here write PAST the actual object, causing stack corruption or memory corruption. See this snippet:

Code:
CCommandLauncherDlg* pDlg = new CCommandLauncherDlg;
delete pDlg;

Yes, that's all I do! And yet it doesn't work. And I cannot figure out why! Does anyone have a clue? I have yet to try to start over with a new project, but...
I can replace those CStrings with other classes, and the same error occours. It happens within the constructors of those objects; native types such as int does not cause this problem. Anyone have ANY idea at all?
 
Last edited:

zAlbee

Keeper of The Iron Tail
Well, it looks like you haven't written a destructor function. That would help.

Also, this may be me confusing C with Java but shouldn't there be brackets after new CCommandLauncherDlg(); ?
 
OP
Doomulation

Doomulation

?????????????????????????
If I put brackets after the function, I would make it inline. It's already declared in the source file.
Good spotting there! I didn't see it did not have a destructor - though I doubt it is the error, I will definetly try it!

EDIT: Hmm, nope, it doesn't work. I will try doing a new project, copying source piece by piece and see what is wrong.
 

blueshogun96

A lowdown dirty shame
I was always told that when allocating memory with the new command in C++ was using the heap and not the stack, am I... wrong? :blush:

EDIT: Does your Deconstructor have a body? I doubt this is the problem but... just asking.
 
Last edited:
OP
Doomulation

Doomulation

?????????????????????????
Yes, new is allocating memory on the heap, but the problem DID occour on both the heap and the stack, because it was modifying past the allocated size. Hence resulting it what is called "stack corruption."

Yes, the destructor has a body, else I would get a linking error.
I doubt anyone knows why it messes up like this, but as I thought it would, it went away after starting the project afresh and copying the source over.
 

Top