|
in my app, I use new operator to alloc memory.
each "new" allocs 200k to 3 M bytes for multiple times, total size alloced around 20M.
the app crashes at a "new operator".
I am wondering why it crashes just for allocing total 20M bytes.
but in Debug mode, the app runs properly. it crashs only in Relese mode, why?
Question:
which way is the best for allocing memory? GlobalAlloc() or new? any explaination?
|
|
|
|
|
Can you add some message boxes (with debugging text) to pause step-by-step each time when it is trying to allocate in release build?
There should be something wrong in your code. Couple days ago I made an application which allocated many of small memory blocks to constitute a 1 GB disk file. I only used new in the code, and it worked just fine.
Maxwell Chen
|
|
|
|
|
includeh10 wrote: the app crashes at a "new operator".
does it give you any message when the crash happens ?
includeh10 wrote: GlobalAlloc() or new?
unless you need the specialized features that GlobalAlloc provides (ex. shared or locked memory), you should use new.
Why donchoo take a peekchur mayn?
OK, cleeeeek
|
|
|
|
|
For the application to crash in the call to "new" one or both of the situations below may be hapening:
1) You call new 1 and then trash the memory thus making new 2 fail because the heap memory list gets corrupted.
2) The new is creating an object which has an error in the constructor and so crashes the application during construction.
To determine what might be happening try the following code:
#include <stdio.h>
void main(void) {
long ttsz=0;
long bsz;
char* bp;
while(1) {
printf("Input size (in KB): ");
scanf("%d", &bsz);
bsz*=1024;
printf("Allocating a size of %d\n", bsz);
bp=new char[bsz];
printf("Allocated a size of %d into 0x%p\n", bsz, bp);
ttsz+=bsz;
printf("Total allocated size (in KB): %d\n", ttsz/1024);
printf("\n");
}
}
I tried this code several times and it works fine.
You should use GlobalAlloc only when strictly necessary. It is not as simple nor as portable as new.
I hope this helps,
Rilhas
-- modified at 17:10 Friday 4th August, 2006
|
|
|
|
|
now i had implemented through PfAddFiltersToInterface(ihandle,1,&inFilter,0,NULL,&fHandle) instead of addglobal filter to interface so that i could specify the ip port+ip to block of my pc.
but it did not work it depends only the parameter passed in pfcreateinterface rather then in addfilter to interface or i might be interpretting the src and destination as wrong. the complete code below
if(::PfCreateInterface(0,PF_ACTION_FORWARD,PF_ACTION_FORWARD,false,true,&ihandle)==NO_ERROR)
{
AfxMessageBox("Step 1 Done");
BYTE ip[4]={202,165,250,50};
BYTE msk[4]={255,255,255,0};
BYTE dm[4]={0,0,0,0};
inFilter.dwFilterFlags = FD_FLAGS_NOSYN; //always this value
inFilter.dwRule = 0; //always this value
inFilter.pfatType = PF_IPV4; //using ipV4 addresses
inFilter.SrcAddr = (PBYTE)dm;
inFilter.SrcMask = (PBYTE)dm; //mask for local ip
inFilter.DstAddr = (PBYTE)ip; //any destination
inFilter.DstMask = (PBYTE)msk;
inFilter.wSrcPort =0;//any source port
inFilter.wSrcPortHighRange=4000;
inFilter.wDstPort = 80; //destination port 80(http service)
inFilter.wDstPortHighRange=80;
inFilter.dwProtocol =(DWORD)FILTER_PROTO_ANY; //Tcp protocol
DWORD err;
err=::PfAddFiltersToInterface(ihandle,1,&inFilter,0,NULL,&fHandle);
if(err==NO_ERROR)
{
if(::PfBindInterfaceToIPAddress(ihandle,PF_IPV4,(PBYTE)ip)==NO_ERROR)
{
AfxMessageBox("IP Adress Binded");
}
}
}
i think input according to input filter it should block on port 80 but i can browse internet through that port.
either i m misunderstanding the src and destination or these api not working.
Tasleem Arif
|
|
|
|
|
Hi guys,
When I am doing my development on MFC, a weird case puzzled me.
I implemented a control which will send a windows message to its parent in some case. For example, it will send its parent a message about 'ON_KEYDOWN' using GetParent()->SendMessage(WM_KEYDOWN.... And I override OnPreTranslateMessage method in its parent control, which I suppose it can catch the WM_KEYDOWN mesasge and do some work on the message.
The code in OnPreTranslateMessage looks like:
void CParentControl::OnPreTranslateMessage(...)
{
if( pMsg->Message == WM_KEYDOWN)
{
....// do some thing here
}
CWnd::OnPreTranslateMessage(..);
}
However, the fact is, in most case OnPreTranslateMessage do can catch the message. But in some case it can not -- and I can sure that the child control absolutely had sent the message.
And I also tried another approach: I use message map to catch ON_KEYDOWN . This time the message can also be catched.
Is there any difference with this 2 approach? In what cases the message will escape from OnPreTranslateMessage but still can be catched by message map?
Sorry I may not describe the question clearly because the limitation of my English.
I really appreciate any one who can give me any clues.
Thanks!
Findekano@Shanghai, China
|
|
|
|
|
Try:
1)test PostMessage() to see what happends - SendMessage() is syncronizied (return after executing the message).
or
2)pass parent window to child, then test again.
such as in parent class, call child->GiveYou(this), in child window, use a class member to record the parent pointer, then use the pointer rather than GetParent().
|
|
|
|
|
Thank you very much for your nice reply.
Actually what your suggested are also my first response.
I tried PostMessage, things does not change...
For the second, I am afraid that invoking a method of a control's parent control is really not a good design. It will make the control only be used inside one certain type of parent control. And actually this is what I want to eliminated -- I think using Windows Message should decouple them a little bit.
Well, this Message Loop thing just remind me another question:
When we hava dialog with a lot of controls in it. And there are even more sub-controls in these controls. The whole tree of the controls could be complex. While we click a button which resides in some place of the control tree, how does the message broadcasted? Will the ON_MOUSE_CLICK bubbled up from the button to its parent, and to its parent's parent, until arrive to the root which is the dialog it self (or even go further) ? Or the message is start from the root node and traved down till the button? Is there any rules of how message is transferred?
I had work on Win32 SDK and MFC for several year, but I still have to say these knowledge is fuzzy to me Hope someone can pull me out from the swampland .
Findekano@Shanghai, China
|
|
|
|
|
Findekano wrote: For the second, I am afraid that invoking a method of a control's parent control is really not a good design. It will make the control only be used inside one certain type of parent control.
This is correct. However attempting to send WM_KEYDOWN isn't any better.
If your needs aren't particularly complicated you can send WM_NOTIFY[^] notification message to the parent. You will be limited to this set of notifications though. If your needs are more complicated define your own custom notification notification message using #define UWM_MYCONTROLNOTIFY WM_APP + x or RegisterWindowMessage() whichever is appropriate for your application.
Findekano wrote: And there are even more sub-controls in these controls
If your controls were properly written in the first place, the fact that a control contains subcontrols shouldn't matter to the parent. Eg. control receives notification from it's subcontrol, if this notification would be of interest to the control's parent, then the control should generate it's own notification message to the parent.
Findekano wrote: Will the ON_MOUSE_CLICK bubbled up from the button to its parent, and to its parent's parent, until arrive to the root which is the dialog it self (or even go further) ?
I'm assuming you mean the WM_LBUTTONDOWN message. Why would you need to bubble raw input messages up to the parent? What will the parent do with them? There is the WM_PARENTNOTIFY message which doesn't exactly bubble but sends to all ancestor windows and gives you notification of button clicks, but I'm sure there is a better way to handle what you are trying to do. (You might want to read the remarks section for WM_PARENTNOTIFY[^]. There aren't all that many messages that windows will forward around if not handled. One of them is WM_MOUSEWHEEL, the default window procedure bubbles it up to the parent if not handled, but this is the exception rather than the norm.
I notice you are trying to use PreTranslateMessage to do something, in most cases (and yours) it looks like a bad design. You might want to read this[^].
-- modified at 14:03 Saturday 29th July, 2006
|
|
|
|
|
Hey,
This is really a lot of help!
Thank you very much!
Jay Zhu@Shanghai, China
|
|
|
|
|
hfry wrote: This is correct. However attempting to send WM_KEYDOWN isn't any better.
If your needs aren't particularly complicated you can send WM_NOTIFY[^] notification message to the parent. You will be limited to this set of notifications though. If your needs are more complicated define your own custom notification notification message using #define UWM_MYCONTROLNOTIFY WM_APP + x or RegisterWindowMessage() whichever is appropriate for your application.
Well, just for make sure of this part.
I just take WM_KEYDOWN as a example (not a good one? ). Here you said 'isn't any better', is it because that I redirect the message to its parent directly w/o any processing. If I can use some user defined message, there has no problem on the design, it this right?
Jay Zhu@Shanghai, China
|
|
|
|
|
Jay Zhu wrote: I just take WM_KEYDOWN as a example (not a good one?).
Actually most of the WM_ messages would be bad to send to the parent. Exceptions would be notification messages eg WM_COMMAND/WM_NOTIFY.
Jay Zhu wrote: If I can use some user defined message, there has no problem on the design, it this right?
Yes this is correct. Take buttons for instance, they don't send WM_LBUTTONDOWN/WM_LBUTTONUP to the parent when clicked, they send a BN_CLICKED notification to the parent so the parent can know exactly what has happened.
|
|
|
|
|
I think i am not very clear about the difference between macro ON_MESSAGE and ON_NOTIFY, anyone who can give me a clear explaination?
It seemed very big, but you can give me any comment you remember.
|
|
|
|
|
JackNotAll wrote: I think i am not very clear about the difference between macro ON_MESSAGE and ON_NOTIFY, anyone who can give me a clear explaination?
ON_NOTIFY is used for handling a particular WM_NOTIFY message, such as LVN_* or TVN_* notifications sent by list view or tree view controls. ON_MESSAGE is used for handling any message that does not have a specialised ON_* macro such as an application-specific mesage.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan Binns:
Thank you so much. I learn from your comment that ON_NOTIFY is used for WM_NOTIFY message, and ON_MESSAGE macro is used for application-specific message, usually a user defined message.
But i still have some questions. I modify an existed Grid Ctrl source code. And i found some WM_NOTIFY message. Because there are some different types cell in grid control, such as edit type, datetime type, and so on. so the existed code use the ON_NOTFY marco to implement these function.
I give your the source code
.h
afx_msg void OnEditCell(NMHDR* pNMHDR , LRESULT* lResult);
.cpp
ON_NOTIFY( CLVN_EDITCHANGE , IDC_LISTVIEWEDIT , OnEditCell)
void CPFGridListCtrl::OnEditCell(NMHDR* pNMHDR , LRESULT* lResult )
{
.......
}
CLVN_EDITCHANGE and IDC_LISTVIEWEDIT is defined by author.
My question is in this situation, why do he use the ON_NOTIFY macro?
And can i implement it by using ON_MESSAGE marcro? perhaps we can, i think i can response the WM_LBUTTONDOWN message and sendmessage in it. Can i implement it by such action?
and i think the question will focus on who send the message. You know, if we define a user message, and we will use SendMessage function to send the message, but if we use ON_NOTIFY, actually, i don't know who send the WM_NOTIFY message to system?
Best Regards
JackNotAll
|
|
|
|
|
JackNotAll wrote: why do he use the ON_NOTIFY macro?
WM_NOTIFY messages contain a sub-message type that indicates the specific type of WM_NOTIFY message that is being sent. In this example, he is handling a WM_NOTIFY message of type CLVN_EDITCHANGE. Since the base message is a WM_NOTIFY, he is using ON_NOTIFY() to handle it. You could use ON_MESSAGE() if you really wanted to, but it's not the best way to do it.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan:
Thank you. I will research them further more.
Best Regards
jackNotAll
|
|
|
|
|
I want to display transparent controls on my dialog-based app.
Normally this is pretty easy: override WM_CTLCOLOR, set transparent background mode and return a hollow brush.
This works fine with all controls *except* checkboxes. For some reason the background of checkboxes will become black. This is very odd since this does not happen with other CButton variations (ie. standard buttons, radiobuttons).
Any suggestions?
|
|
|
|
|
did you use CTLCOLOR_STATIC for check-box?
|
|
|
|
|
No, I don't check the control type because all controls have to be transparent.
So normally the code below should work for checkboxes but it doesn't
HBRUSH CTest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
pDC->SetBkMode(TRANSPARENT);
return hbr;
}
|
|
|
|
|
I'm seeing the same problem. Checkboxes are drawing with a black fill colour when the brush is set to HOLLOW_BRUSH.
Was a solution ever found?
|
|
|
|
|
Hi,
I have written a VC++ 6.0 program to send data through the serial port.I
have successfully sent the ASCII characters using my code.But now I need to
send defined block of bits (eg. "1100110") through the serial port.Is it
possible. Also if there is any problem I can add some overhead bits and make
the number of bit to 8 so that it can be defined as a byte,
I will be really happy if someone of you can help me regarding the above
matter.For your conveience I have given below some parts (open port and
sending data) of the code that I am using.
//To open the port and Configure
HANDLE handlePort_; // the object that is a instace of port.
DCB config_1;
BOOL openPt(BOOL statusPort_1,const char* portName)
{
// Defaults
config_1.ByteSize = 8; // Byte of the Data.
config_1.StopBits = ONESTOPBIT; // Use one bit for stopbit.
config_1.Parity = NOPARITY; // No parity bit
config_1.BaudRate = CBR_9600; // Buadrate 9600 bit/sec
//---------------------------------------
if (statusPort_1 == FALSE) // if port is opened already, not open port
again.
{
handlePort_ = CreateFile(portName, // Specify port device: default "COM1"
GENERIC_READ | GENERIC_WRITE, // Specify mode that open
device.
0, // the devide isn't shared.
NULL, // the object gets a default
security.
OPEN_EXISTING, // Specify which action to
take on file.
0, // default.
NULL); // default.-
// Get current configuration of serial communication port.
if (GetCommState(handlePort_,&config_1) == 0)
{
AfxMessageBox("Problem occured in configuration.");
return FALSE;
}
config_1.ByteSize = 8; // Byte of the Data.
config_1.StopBits = ONESTOPBIT; // Use one bit for stopbit.
config_1.Parity = NOPARITY; // No parity bit
config_1.BaudRate = CBR_1200; // Buadrate 1200 bit/sec
// Set current configuration of serial communication port.
if (SetCommState(handlePort_,&config_1) == 0)
{
AfxMessageBox("Set configuration port has problem.");
return FALSE;
}
// instance an object of COMMTIMEOUTS.
COMMTIMEOUTS comTimeOut;
// Specify time-out between charactor for receiving.
comTimeOut.ReadIntervalTimeout = 3;
comTimeOut.ReadTotalTimeoutMultiplier = 3;
comTimeOut.ReadTotalTimeoutConstant = 2;
comTimeOut.WriteTotalTimeoutMultiplier = 0;
comTimeOut.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(handlePort_,&comTimeOut);
// Updata port's status.
return TRUE;
}
else
{
AfxMessageBox("Port is Already Opened");
return TRUE;
}
}
// to write data to the port
void writePort(LPCVOID outputData,
const unsigned int& sizeBuffer,
unsigned long& length)
{
if (length > 0)
{
if (WriteFile(handlePort_, // handle to file to write to
outputData, // pointer to data to write to file
sizeBuffer, // number of bytes to write
&length,NULL) == 0) // pointer to number of bytes written
{
AfxMessageBox("writing of serial communication has problem.");
}
}
}
Tdk
|
|
|
|
|
How is your defined block of bits stored? Unless it is some character representation of a bit string, you can send the data as-is. For example, suppose you could do the following, using your code:
unsigned long defined_bits = 0x00000023;
unsigned long length;
writePort(&defined_bits,sizeof(defined_bits),length); This would write four bytes out the serial port: 0x23 , 0x00 , 0x00 , and 0x00 .
Let's suppose for a minute your 'defined bits' are in the form of a character string ('1', '1', '0', '0', '1', '1', '0' for example). Each byte you send is 8 bits. Convert your string of bits to bytes, 8 at a time, and send each byte.
char defined_bits[] = { '1', '1', '0', '0', '1', '1', '0' };
unsigned char data = 0x00;
unsigned char mask = 0x01;
for (int bit_index = 0; i < sizeof(defined_bits); bit_index++) {
if (defined_bits[bit_index] == '1') data |= mask;
mask <<= 1;
if (mask == 0x00) {
unsigned long length;
writePort(&data,sizeof(data),length);
data = 0x00;
mask = 0x01;
}
}
Software Zen: delete this;
|
|
|
|
|
Thanks Gary,
I implemented the hint you gave as follows.
<br />
char defined_bits[] = { '1', '0', '0', '0', '0', '0', '0', '0' };<br />
defined_bits[5] = '1';
<br />
unsigned char data = 0x00;<br />
unsigned char mask = 0x01;<br />
for (int bit_index = 0; bit_index < sizeof(defined_bits); bit_index++) <br />
{ <br />
if (defined_bits[bit_index] == '1') data |= mask; <br />
mask <<= 1; <br />
}<br />
CString w = CString(data);<br />
CString header = "ABCDE";<br />
<br />
CString tel6 = header +w ;<br />
<br />
unsigned long length;<br />
writePort(tel6,tel6.GetLength(),length);<br />
<br />
Also I changed the individual bits as ;
defined_bits[1] = '1';
Thanks a lot
TDK
ENTC
UoM
|
|
|
|
|
hi, i'm kind of new to the VC++ environment...
i have made my own class to add onto another program. my class creates a CFrameWnd and draws stuff on it. the problem is that i have scroll bars on the window, and i can not get my class to recognize the message maps (it acts like it never happened)...
this is my constructor and a small message box i made to try to catch the horizontal scroll bar message.
can any one help me figure out what i am doing wrong? thanks a bunch!
Bongo::Bongo(int X, int Y)
{
m_nScreenSizeX = X;
m_nScreenSizeY = Y;
RECT rectWindow;
rectWindow.top = 30;
rectWindow.bottom = rectWindow.top + m_nScreenSizeY;
rectWindow.left = 30;
rectWindow.right = rectWindow.left + m_nScreenSizeX;
BongoFrame = new CFrameWnd();
BongoFrame->Create(NULL, "Bongo App", WS_OVERLAPPED|WS_VSCROLL|WS_HSCROLL, rectWindow, NULL, NULL);
BongoFrame->ShowScrollBar(SB_BOTH,TRUE);
BongoFrame->ShowWindow(SW_SHOW);
BongoFrame->UpdateWindow();
BongoFrame->GetClientRect(rcClient);
pDC = BongoFrame->GetDC();
}
void Bongo::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
AfxMessageBox("pop");
CFrameWnd::OnHScroll(nSBCode, nPos, pScrollBar);
}
|
|
|
|
|