ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  MFC multiple windows
Posted by Robert Merwa on Jul-20-2018 15:59
I am using Visual Studio with MFC and I have multiple Dialog windows open. I want to draw in each of these windows different charts. I try different implementations (two CChartViewers are not possible) but I do not manage it.

Thanks in advance.
Regards,
Robert Merwa

  Re: MFC multiple windows
Posted by Peter Kwan on Jul-21-2018 02:20
Attachments:
Hi Robert,

I have just copied the dialog in the "traclaxis" sample code to the "helloworld" sample code. In this way, the "helloworld" sample code will have two dialogs showing different charts. I added the code to display the two dialogs at the same time and everything works normally.

I have attached the modified "helloworld" sample code for your reference. You can copy the files inside to the original "ChartDirector/mfcdemo/helloworld" directory and run the Visual Studio solution to try it.

If you need further help, would you mind to clarify exactly what is the issue? In the "ChartDirector/mfcdemo/mfcdemo" sample code, there is 8 CChartViewers on the dialog and they can show different charts depending on user selections.

Regards
Peter Kwan
helloworld.zip
helloworld.zip

39.00 Kb

  Re: MFC multiple windows
Posted by Robert Merwa on Jul-21-2018 20:59
Hi Peter,

thank you very much for the quick response and the attachement.

My request before was too short and the problem description not very good.

Short description of my project:

At present I realized my own heating control. The hardware is already finished and now I want to visualize all time-dependent parameters.

Software:
I have one main Dialog window where I can see the actual temperatures (buffer storage, inlet temperatures,...) and the actual settings - this part is working.
Now I can open in this main Dialog window additional Dialog windows by pressing several buttons.
And I want to display in one window the temperatures of the buffer storage, in an other window the floor-temperatures and so on ...

Thus I create for each additional window a seperate class and in the main Dialog window I open it with:

  PlotWindow* pDlg = new PlotWindow(this);
  if (pDlg->Create(IDD_PLOTWINDOW, this))
    pDlg->ShowWindow(SW_SHOWNORMAL);
  else
    delete pDlg;

In each several class I define a seperate CChartViewer (+ more or less a copied code from your projects to visualize anything at the beginning), but if I create it, the Address of the CChartViewer is 0x00000000 .


Anyway ..., I can realize my project with your attachement (thanks once again) but can you help me, how I can send/receive data (data exchange) between these two dialog windows. If this works, I beleive my problem is solved.


many thanks
regards
Robert Merwa

  Re: MFC multiple windows
Posted by Peter Kwan on Jul-22-2018 16:57
Hi Robert,

I am thinking, it should be impossible for the address of anything be 0x00000000 in a computer. Only a pointer can be 0x00000000, in which case it just means it is not pointing to anything in C++.

You mentioned you create a CChartViewer. Without seeing your code, it is hard to know how you create it. In our own sample code, a part of which is generated by Visual Studio Class Wizard, the CChartViewer is created as a member variable of the CDialog like:

CChartViewer m_ChartViewer;

If the dialog exists and is not null, it does not seem possible for "&m_ChartViewer" (address of the CChartViewer) to be 0x00000000. Are your code actually creating a CChartViewer, or is it creating a pointer to a CChartViewer (like CChartViewer *m_ChartViewer)? A pointer can be 0x00000000 if it does not pointer to a CChartViewer that your code created somewhere else, but the address of CChartViewer itself (or of any other object in C++) cannot be 0x00000000.

Like any other controls in the CDialog, first there should be an object CChartViewer. It needs to connect to the control in the dialog resource. This is usually done in the  DoDataExchange method of the CDialog via DDX_Control. If the CChartViewer is generated by the Visual Studio Class Wizard, these code should be automatically generated by the Class Wizard. If you are not using dialog resource, you need to use CStatic::Create to create the control. This is the same for all MFC controls.

You can use the control after its container (the CDialog) is initialized. Normally, you can use the control in CDialog::OnInitDialog and thereafter. You should not use the control before the containing dialog has been initialized (before OnInitDialog). In our sample code, everything is done in OnInitDialog or after it. If the CChartViewer is used before OnInitDialog, some internal MFC assertion may result claiming some internal MFC variables (probably a Window handle) is NULL.

If the above still cannot solve the problem, it is possible to provide a complete example that I can run (perhaps by modifying the helloworld sample code) so I can try to reproduce the issue?

Regards
Peter Kwan

  Re: MFC multiple windows
Posted by Robert Merwa on Jul-22-2018 21:05
Hi Peter,

many many thanks - it works now.
I did a very silly mistake and implement the chart code in the PlotWondow function.
Sorry if I use the incorrect terminology in C++, but I am a low level programmer (only C-Code) and work with C++ just a few weeks.

Your support is excellent and once again thank you very much for the quick response.

best regards from Austria
Robert Merwa

  Re: MFC multiple windows
Posted by Robert Merwa on Jul-29-2018 00:28
Attachments:
Hi Peter,

I implemented a seperate Window with an online tracking MultiChart with 4 XYCharts.
It works but the memory of the computer is increased (about 3 MB) after each tracking-step.
The code is a mix of the mfcdemo-projects. After about 4 Minutes the memory is full and the program is finished. For a window with only one XYCHART the online tracking works, but for a MultiChart within a Window the memory overflows. I have no idea which objects should be deleted after a tracking-step.

Attached you can find the cpp-Code of the online-tracking MultiChart.

many thanks in advance for the help
Regards
Robert
MFC_Chart3.rar
MFC_Chart3.rar

8.45 Kb

  Re: MFC multiple windows
Posted by Peter Kwan on Jul-29-2018 14:58
Hi Robert,

In C++, if your code allocate memory or objects (eg. using "new"), your code must free the object (eg. using "delete"). Normally, it is not possible for other code to free the memory allocated by for your code.

It is because there are many ways to allocate memory in C++ (new, placement new, malloc, GlobalAlloc, and many other custom allocators, stack). Other code (such as ChartDirector) cannot know how your code allocates the memory in the first place, so it cannot know how to free the memory. Your own code must free the memory.

In your code, I can find 5 "new" allocations:

MultiChart *m = new MultiChart(1200, 600);
XYChart *chart1 = new XYChart(920, 260);
XYChart *chart2 = new XYChart(920, 130);
XYChart *chart3 = new XYChart(920, 130);
XYChart *chart4 = new XYChart(920, 130);

However, your code only have one "delete". So 4 of the above 5 charts are leaked. (Probably all the XYChart are leaked as your code does not delete them.) To solve the problem, please modify your code to ensure every "new" must match with a "delete".

Hope this can help.

Regards
Peter Kwan

  Re: MFC multiple windows
Posted by Robert Merwa on Jul-29-2018 22:10
Hi Peter,

thank you very much for the quick response.
I know, that I have to delete the objects which were created with the command new, but e.g. in the mfcdemo-project "realtimezoomscrollDlg.cpp" in line 500 ( CRealtimezoomscrollDlg::drawChart(CChartViewer *viewer) ) you create the object c by means of:  XYChart *c = new XYChart(640, 350); and I cannot find anywhere in the code the line "delete c";
I tried to delete the MultiChart-Object and the XYChart-Objects at the end of the function "void MFC_Chart3::drawChart(CChartViewer *viewer)".
Compiling was ok but if I run the executable file I get the Visual Studio Info:
Unhandled exception at 0x5E2A8CF8 (chartdir60.dll) in MFC_UDP_Server1.exe: 0xC0000005: Access violation reading location 0x00000000.
I beleive, it is the wrong place to delete these objects, but I have to say quite honestly that I have no idea where I should delete these objects in the code during online tracking.

many thanks in advance for the help
Regards
Robert