|
It would be quite difficult to do. Probably can't do it at all with a straight up CMap.
I think the only way you could share something that dynamic across multiple processes would be to set up a memory-mapped file, and map the new and delete operators for your class to use memory from that file or something.
I had a similar problem a while ago, and ended up abandoning my design because of it.
Even a broken clock is right twice a day.
|
|
|
|
|
Good day!
I have a class that holds my data.
the part of the data is used to fill a menu ( in a toolbar ), and to fill a listbox ( in a controlbar ). the data is used with the menu command, and when the user d-click in the list.
The menu and the listbox are not directly members of the mainframe and are not UI related, and the menu handlers are handled in the CView ( it's multiple view application );
I'm still not sure where to put the data ? as part of the MainFrame since it's from there I'll create the menu and the listbox, and pass a pointer to each ctor ( or some init method ). or as a global singleton ( will pollute the namespace )?
Thanks
Max.
|
|
|
|
|
I suppose that if you're following the traditional MFC Document/View architecture, that your data should reside in your CDocument-derived class. If that's too constrictive, put in your CWinApp-derived class, since it's a global singleton anyway.
|
|
|
|
|
Hi,
I have an application that adds lots of stuff into a list box.
How can I make sure that always the last item that was added will be displayed at the bottom of the list box without having to scroll to it ?
Thanks
Shay Harel
|
|
|
|
|
m_List.EnsureVisible( m_List.GetItemCount()-1, FALSE );
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
Well, sounds great but.....
I dont see this method as a member function of CListBox .
Any more ideas ?
Shay
|
|
|
|
|
It's actually a member of CListCtrl. You may consider switching to that instead -- it gives you more options. But if you can't, try using SetCurSel to select the last item. I don't remember if it causes the list to be automatically scrolled.
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
You should start using CListCtrl instead of CListBox. It has more features and, personally, I think it is easier to work with.
CListBox:
m_List.SetCurSel(m_List.GetCount()-1);
Win32 (works in any version of MFC):
Here is the 'old school' Win32 macro way to ensure that the last item is displayed (MFC is just a wrapper around Win32 calls, mostly):
ListView_EnsureVisible( hList, ListView_GetItemCount(hList)-1, FALSE );
Also, the macros ListView_EnsureVisible() and ListView_GetItemCount() are defined in commctrl.h
'F1' can go a long way...
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
I think what you need is (win32) LB_SETTOPINDEX. I think that's what it's called anyway.
|
|
|
|
|
I am a new programmer, can't figure out why the keyboard doesn't responds. I traced the following code, the doesn't enter the code corresponding to keyboard at all.
Here I attached how the dialog is triggered. Hopefully someone can help me figure it out. Thanks a lot.
int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow)
{
.....
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
....
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper);
}
BOOL CALLBACK Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_CHAR:
switch(wParam)
{
case '1':
record_data_pair(1);
break;
case '2'://0x32:
record_data_pair(2);
break;
case '3'://0x33:
record_data_pair(1);
break;
default:
break;
}
return FALSE;
}
.....
return FALSE;
}
Sean
|
|
|
|
|
I think the problems lies in this part of your code...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper);
If you specify 'NULL' as owner of this window, the Desktop will be the parent. However, since this dialogbox doesn't have it's own messageque it won't receive anything.
So, if you do it like this, it should work just fine.
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Exper);
Where 'hwnd' is the handle from your main window (in MainWndProc)
Don't think you are, know you are...
|
|
|
|
|
Shall I declare a global HWND hmainwnd; and within the WinMain() function.
if (!(hwnd = CreateWindow ("MAINWINDOW", "1DoF Stepper Motor Haptic Device",
WS_OVERLAPPEDWINDOW,
0,0,
800, 150,
NULL, NULL, hCurrentInst, NULL)))
return (0);
hmainwnd = hwnd;//like this???
and then when triggering the dialog, I do
hwnddlg = CreateDialog(app_instance, "Experiment", hmainwnd, (DLGPROC) Parameter_Window_Exper);
But I did so, it doesn't work.
The sequence triggered is as following: Main->Dialog_Data->Dialog_Experiment. I want the Dialog_Experiment to respond my keyboard.
My code(simplified) is like this:should I change all NULL to hmainwnd?
~~~~~~~~~~~~~~~~~~~~~~~~~~~`
extern HWND hwnddlg;
int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow)
{
...
if (!RegisterClass (&wc)) {
MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed",
(LPCTSTR) "Error!", MB_OK | MB_ICONEXCLAMATION);
return(FALSE);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
------------------------------------------
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Data);
}
-------------------------------------------
BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
...
DestroyWindow(hWnd);
hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, NULL, (DLGPROC) Parameter_Window_Exper);
}
-------------------------------------------
BOOL Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
case WM_CHAR:
switch(wParam)
{
case '1':
MessageBox(hmainwnd, "Happen?", "TEST Box", MB_OK);
record_data_pair(1);
break;
case '2'://0x32:
record_data_pair(2);
break;
case '3'://0x33:
record_data_pair(1);
break;
default:
break;
}
return FALSE;
}
Thanks
Sean
|
|
|
|
|
I don't think you have to declare a global variable.
This should do the trick (and if not, I'll need to see more of your code):
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Data);
}
-------------------------------------------
BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
...
DestroyWindow(hWnd);
hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, hWnd, (DLGPROC) Parameter_Window_Exper);
}
-------------------------------------------
You see, just give it the handle to the parent in stead of using NULL (which means the desktop)
Don't think you are, know you are...
|
|
|
|
|
I did so as you posted, but the child dialog (EXPER_INTRO_DIALOG) can't be displayed. I wonder if the problem now(not display at all) is because DestroyWindow(hWnd) has killed the parent dialog.
Anyway, I guess I can get my program done under your help.
Regards,
Sean
|
|
|
|
|
Hello gurus,
I have a similar question to what was discussed
here
Suppose I have a function to split a text file into 10 parts like (The first 9 parts are 90% of the total text and the last part contains whatever remaining).
When the dialog closes I have a memory leak problem related to pBuffer which I'm not releasing, as is expected. How and where do I free pBuffer?
the code goes like(not too commented)
BOOL CReadPercentDlg::ReadPercent(LPCTSTR strFileName)<br />
{<br />
HANDLE hTheFile;<br />
hTheFile = CreateFile(strFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);<br />
if(!hTheFile)<br />
return FALSE;<br />
DWORD dwFileSize = ::GetFileSize(hTheFile, NULL );<br />
DWORD dw10Percent = (DWORD)( dwFileSize *.10 );<br />
BYTE *pBuffer = new BYTE[ dw10Percent ];<br />
<br />
if( !pBuffer )<br />
{<br />
TRACE("Not enough mmemory");<br />
return FALSE;
}<br />
<br />
DWORD dwStatus = 0;<br />
DWORD dwRead = 0;<br />
DWORD dwTotalRead = 0;<br />
for(int i=0;i<9;i++)<br />
{<br />
<br />
dwStatus = ::ReadFile( hTheFile, pBuffer, dw10Percent, &dwRead, NULL );<br />
if( dwStatus != ERROR_SUCCESS )<br />
{<br />
<br />
} <br />
<br />
pBuffer[dw10Percent] = '\0';<br />
OutputDebugString((LPSTR)pBuffer);<br />
dwTotalRead+=dw10Percent;<br />
}<br />
<br />
pBuffer[0] = '\0'; <br />
pBuffer = NULL;<br />
DWORD dwLeft = 0;<br />
dwLeft = dwFileSize - dwTotalRead;<br />
if(dwLeft!=0)<br />
{<br />
TRACE("\nLeft %u",dwLeft);<br />
<br />
pBuffer = new BYTE[dwLeft];<br />
if(pBuffer==NULL)<br />
return FALSE;<br />
<br />
dwStatus = ::ReadFile( hTheFile, pBuffer, dwLeft, &dwRead, NULL );<br />
if( dwStatus != ERROR_SUCCESS )<br />
{<br />
<br />
<br />
<br />
} <br />
pBuffer[dwLeft] = '\0';<br />
TRACE("\nLast :\n");<br />
TRACE((LPSTR)pBuffer);<br />
<br />
}<br />
<br />
pBuffer = NULL;<br />
return TRUE;<br />
<br />
}
Also, is there any better way to read in such percents of text files?
Thanks a lot in advance.
Looking for a lurking sig!
|
|
|
|
|
smartptr wrote:
(The first 9 parts are 90% of the total text and the last part contains whatever remaining).
smartptr wrote:
Also, is there any better way to read in such percents of text files?
If you used C++ file handling ( iostreams ), you could read into std::strings, which would clean up after themselves. Otherwise, you need to delete [] the buffer at the end of this function. There is no-where else to do it, you create it on the heap within this function, therefore once you leave this function, the pointer has leaked, you no longer have a reference to it.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
Thanks for your reply.
Can I make the buffer a member variable of the dialog class?(and when the dialog is closing:
delete pBuffer;<br />
pBuffer = NULL; )
to avoid a memory leak?
|
|
|
|
|
Sure - no problems at all, except that you need to delete [] pBuffer, not delete it, it is an array.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
smartptr wrote:
//where do we free pBuffer?
//following line again causes
//memory check error at 0x004228B7 = 0x00, should be 0xFD.
// delete[] pBuffer;
//but if I do not release pBuffer by now, I get a memory leak.
It could be that there is a memory problem elsewhere inthe code that is trashing something and causing the delete to fail. This line looks suspicious:
<br />
pBuffer[dw10Percent] = '\0';<br />
It looks like you are accessing the array beyond its end. You allocated it as having dw10Percent items, which means the highest index it can have is (dw10Percent - 1).
There may be other things like that, too, that are causing problems.
Even a broken clock is right twice a day.
|
|
|
|
|
I have an MDI application and I implement the serialize function to read data in . In case the File read is not a trusted file , how do i abort the process of displaying the view. i want the document to not show the view but to just display an error message. Thank you .
|
|
|
|
|
I'm french, so I hope you will excuse me for my poor english.
I have an MFC application which load lot of DLL. In this Dll I make allocation with new, so It use the MFC checking system for leak memory.
And when I terminate my application, I free all my librairies with function 'FreeLibrary()'. But If I have some leak memory in a DLL the MFC Dumping Leak will display to me :
Dumping Object->
#File Error#, etc....
I don't have the FileName.
MFC can't find the name of the File (__FILE__) because the memory where the FileName where store was allocate in the DLL where the 'new' is made. So when I call the function 'FreeLibrary()' on my DLL, all memory which containt '__FILE__' were release.
So how can I do to see my memory leak even if i have call FreeLibrary ? (I must call function FreeLibrary() otherwise I will have lot of problems with my application).
Thanks you for reading my question.
|
|
|
|
|
leander wrote:
So how can I do to see my memory leak even if i have call FreeLibrary ? (I must call function FreeLibrary() otherwise I will have lot of problems with my application).
I am not biblically familiar with MFC as I had to lookup exactly what FreeLibrary() does. Anytime you use the new operator to allocate memory on the heap you will need to release it with the delete operator. It appears that the FreeLibrary *only* handles ref counting and will unmap the module from the address space itself, I would belive that you still need to handle the internal allocation/deallocation of memory yourself. Hope this helps.
Nick Parker
Not everything that can be counted counts, and not everything that counts can be counted. - Albert Einstein
|
|
|
|
|
Cant you turn off FreeLibrary, only for so long as to find the memory leak?
Also, Are the DLLs in debug mode? To completely debug the application, both your application and DLLs should be built in Debug mode.
|
|
|
|
|
I try to turn off the call() to FreeLibrary() to Debug my leak, but it's little complicate to do it, because I have static object how make allocation with a memory manager (only for malloc and free, not for new and delete). So if I don't call the FreeLibrary the destructor off my static object is not call, and i have some crash when I free my memory manager.
So for the moment, to debug leak, I remove the Freeliberary() and I don't release my memory manager to see the leak.
But I hope to find an other solution without changing some code to with memory leak.
My application and my DLL are compile in Debug with option "Multithread DLL"
|
|
|
|
|
I don't understand why failing to call FreeLibrary causes a lot of problems in your app. But that's what I would do: comment out the FreeLibrary calls (temporarily) and see if you can get a better reading on where the leaks are occurring.
If that doesn't work, let me know and I'll help you find the leaks the hard way.
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|