|
|
Sometimes, while allocating memory in my programs, I get NULL in responce to call of new operator.
Sometimes, when I execute following pair of statements, I get Heap Error.
TCHAR * szText = new TCHAR[iLen * 7] ;
//... Some Code
delete [] szText ; // Causes heap error
Where and what I might be doing wrong?
Polite Programmer
More Object Oriented then C#
|
|
|
|
|
What is the value of iLen ? And what is the code missing between these two lines. You don't provide any valuable information for us to help you...
|
|
|
|
|
You are right, not enough information:
Value of iLen is > 0 < 200. Moreover, between the lines, we are just calling string manipulation functions like strlen, strcpy etc. But when the delete []szText is executed, heap corruption message follows....
Polite Programmer
More Object Oriented then C#
|
|
|
|
|
Can you post the exact code ? We cannot help if you don't provide meaningfull informations.
|
|
|
|
|
Polite Programmer wrote: between the lines, we are just calling string manipulation functions like strlen, strcpy etc.
What if these are omitted? Does the problem still exist? If not, it could be that you are writing outside of the allocated memory.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Polite Programmer wrote: I get NULL in responce to call of new operator.
Is this the problem?
Also try to use Safe String APIs instead of strcpy etc
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Here is the full code, Comments starting with NOTE are the erroroneous...
<br />
#include <windows.h><br />
#include <tchar.h><br />
#include <stdio.h><br />
<br />
#include "resource.h"<br />
<br />
#include "convert.h"<br />
<br />
HWND hwndNextClipboardVierwer ;<br />
char * szInPageData ;<br />
HFONT hFont ;<br />
<br />
<br />
<br />
<br />
void DumpHex(TCHAR * szText, HWND hDlg)<br />
{<br />
int iLen = _tcsclen(szText) ;<br />
TCHAR szBuffer[10] ;<br />
<br />
<br />
TCHAR * szHexDump = new TCHAR[iLen * 7] ;<br />
<br />
ZeroMemory(szHexDump, iLen * 7) ;<br />
<br />
for(int i = 0 ; i < iLen ; i++)<br />
{<br />
wsprintf(szBuffer, _T("0x%X, "), szText[i]) ;<br />
_tcscat(szHexDump, szBuffer) ;<br />
}<br />
<br />
SetDlgItemText(hDlg, IDC_EDIT_UNIHEX, szHexDump) ;<br />
<br />
<br />
delete [] szHexDump ;<br />
}<br />
<br />
BOOL CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)<br />
{<br />
HANDLE hMemory = NULL;<br />
TCHAR * szUnicodeText = NULL ;<br />
char * lptstr = NULL ;<br />
int iLen = NULL ;<br />
<br />
switch(msg)<br />
{<br />
case WM_INITDIALOG:<br />
<br />
hwndNextClipboardVierwer = SetClipboardViewer(hWnd) ;<br />
hFont = CreateFont( 80,<br />
80,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
0,<br />
_T("Pak Nastaleeq")) ;<br />
SendDlgItemMessage(hWnd, IDC_EDIT_UNICODE, WM_SETFONT, (WPARAM) hFont, TRUE) ;<br />
return FALSE ;<br />
<br />
case WM_DRAWCLIPBOARD:<br />
<br />
if(OpenClipboard(hWnd))<br />
{<br />
hMemory = GetClipboardData(CF_TEXT) ;<br />
lptstr = (char *) GlobalLock(hMemory) ;<br />
<br />
if(hMemory == NULL || lptstr == NULL)<br />
{<br />
CloseClipboard() ;<br />
return FALSE ;<br />
}<br />
<br />
iLen = strlen(lptstr) ;<br />
szInPageData = new char[iLen + 1] ;<br />
szUnicodeText = new TCHAR[(iLen / 2) + (iLen / 100) * 10] ;<br />
strcpy(szInPageData, lptstr) ;<br />
GlobalUnlock(hMemory) ;<br />
<br />
SendMessage(hwndNextClipboardVierwer, msg, wParam, lParam) ;<br />
ConvertInPageToUncode(szInPageData, iLen, szUnicodeText) ;<br />
DumpHex(szUnicodeText, hWnd) ;<br />
SetDlgItemText(hWnd, IDC_EDIT_UNICODE, szUnicodeText) ;<br />
<br />
delete [] szInPageData ;<br />
<br />
delete [] szUnicodeText ;<br />
<br />
<br />
}<br />
<br />
<br />
return TRUE ;<br />
<br />
case WM_COMMAND:<br />
switch(LOWORD(wParam))<br />
{<br />
case IDOK:<br />
EndDialog(hWnd, 1) ;<br />
break ;<br />
case IDCANCEL:<br />
EndDialog(hWnd, 0) ;<br />
break ;<br />
<br />
}<br />
return TRUE ;<br />
<br />
case WM_DESTROY:<br />
ChangeClipboardChain(hWnd, hwndNextClipboardVierwer) ;<br />
return TRUE ;<br />
}<br />
<br />
return FALSE ;<br />
}<br />
<br />
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,<br />
LPTSTR lpCmdLine, int nCmdShow)<br />
{<br />
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc) ;<br />
return 0 ;<br />
}
Polite Programmer
More Object Oriented then C#
|
|
|
|
|
Polite Programmer wrote: void DumpHex(TCHAR * szText, HWND hDlg)
{
int iLen = _tcsclen(szText) ;
TCHAR szBuffer[10] ;
/*NOTE: This call fails i.e. returns NULL (even iLen is > 0)*/
TCHAR * szHexDump = new TCHAR[iLen * 7] ;
ZeroMemory(szHexDump, iLen * 7) ;
for(int i = 0 ; i < iLen ; i++)
{
wsprintf(szBuffer, _T("0x%X, "), szText[i]) ;
_tcscat(szHexDump, szBuffer) ;
}
/*NOTE: This Call generates Heap corruption message from CRT*/
delete [] szHexDump ;
}
This is the code snippet that you need to focus on. What is the value of ilen ? What does szBuffer look like each iteration of the loop? How are you verifying that szHexDump is big enough for iLen concatenations?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I think you are mixing to many types of chars (i.e TCHARs, chars and LPSTRs). I think u have a confusion regarding there usage. First of all from ur code....
TCHAR *szUnicodeText = new TCHAR[(iLen / 2) + (iLen / 100) * 10] ;
you give an impression that TCHAR == Unicode character???? this is a wrong assumption. The definition of TCHARs can be found in headers its something like
<br />
#if defined _UNICODE<br />
#define TCHAR WCHAR<br />
#else<br />
#define TCHAR CHAR<br />
#enif<br />
so depending upon your build type (unicode or ansi) the type and hence the size of the TCHARs changes.
I think you are trying to make some sort of clipbroard viewer to display the unicode and hex value of the data on the clipboard.
try using this code instead in your DrawClipboard notification
<br />
if(OpenClipboard())<br />
{<br />
HANDLE hMemory = GetClipboardData(CF_TEXT) ;<br />
TCHAR *lptstr = (TCHAR*) GlobalLock(hMemory) ;<br />
<br />
if(hMemory == NULL || lptstr == NULL)<br />
{<br />
CloseClipboard() ;<br />
}<br />
<br />
int iLen = _tcslen(lptstr) ;<br />
TCHAR *szInPageData = new TCHAR[iLen + 1] ;<br />
_tcscpy(szInPageData, lptstr) ;<br />
USES_CONVERSION;<br />
LPWSTR szUnicodeText = T2W(szInPageData);<br />
GlobalUnlock(hMemory) ;<br />
<br />
::SendMessage( hNextViewer, WM_DRAWCLIPBOARD, 0, 0 ) ;<br />
DumpHex( W2T(szUnicodeText), GetSafeHwnd()) ;<br />
<br />
delete [] szInPageData ;<br />
}<br />
Trust me, nothing's impossible!
|
|
|
|
|
Polite Programmer wrote: //... Some Code
Is it possible that "//... Some Code" writes more than iLen * 7 into szText?
I.e. the terminating '\0' character?
Or that "//... Some Code" causes a corrupted stack?
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
I'm sure it does'nt write past iLen * 7 but I'm not sure whether it can corrupt the stack. Please tell me how code can corrupt the stack? (Becuase I really dont know if the question seem stupid)
Polite Programmer
More Object Oriented then C#
|
|
|
|
|
Hello.
IMO, The usual reason for stack corruption is this:
foo
{
char text[5];
strcpy(text, "Hello");
}
text is a stack variable of 5 chars, but the strcpy() copies 6 bytes to it.
The last character written (the '\0' byte) will be written outside the boundry of text.
******** THIS IS A SIMPLIFIED EXPLANATION ********
The return address (to the function that called foo()) is also saved on the stack.
When the function foo returns, the return address is picked up from the stack. But a part of the return address is now overwritten by the the '\0' byte, and is altered.
So the program retuns to the wrong address, causing the program to go bananas.
That's stack corruption.
-- modified at 3:06 Wednesday 27th September, 2006
But you can get a lot of different fishy behaviour from stack corruption. It depends how the compiler uses the stack. It doesn't have to result in wrong return address, other things can happen, such as another stack variable suddenly changes its value, e.t.c.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hi,
I used property sheet method to create two property pages: PageA and PageB.
I edited some values in PageA,but did not click save button, then I went to PageB.What should I do to see my previous value in PageA when I came back from PageB?
Thank you in advance,
yijia
-- modified at 2:16 Wednesday 27th September, 2006
|
|
|
|
|
yijia_24 wrote: What should I do to see my previous value in PageA when I came back from PageB?
Nothing special. Values are retained by default. What does the OnSetActive() method look like for PageA?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thank you very much. I solved that problem.
I appreciate you spending time over my question.
|
|
|
|
|
yijia_24 wrote: I solved that problem.
Do you mind sharing your solution with others?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
so sorry about that. I think my method doesnt work... I do not have onSetActive() in my program. Would you tell me more detail about onSetActive()?
Thank you very much!
|
|
|
|
|
yijia_24 wrote: Would you tell me more detail about onSetActive()?
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
I have a MS Chart Control. I process data dynamically and put 10 Frames of data on it every second. For which the timer value should be 100 MilliSeconds. The problem is that since timer value is low the flickerring rate is high and visible. I cannot increase the timer.
Is there any other way to bring down the flickering effect.
Thanks in Advance.
Regards
Kenny.
|
|
|
|
|
Did you try double buffering? There are heaps of samples, at CP and elsewhere.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
hi ,
i have a project in borlands c++ to be converted to vc++.
The package concept in borlands c++ uses vcl(visual component library)
how to use this default library of borlands c++ in vc++???
if used is it possible to use the package concept in vc++.
reply immediately,
thanks in advance,
zari
|
|
|
|
|
zareee wrote: reply immediately,
But of course, what else would I be doing ?
zareee wrote: how to use this default library of borlands c++ in vc++???
I doubt you can.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
hav u ever used this??
i am struck up with how to use VCL in vc++
can u please help me out in this
zari
|
|
|
|
|
I'm always interested when I answer people and they ask the same question again.
Why would Microsoft support Borland's libraries ? If it can be done, it would involve a lot of hacking. If you have the full source for the VCL, you may be able to use it as a library in your code, but again, I doubt it.
I've googled ( I assume you have too ? ) and I can't see anything. I think your chances are low.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|