question

IDH-0162 avatar image
0 Votes"
IDH-0162 asked IDH-0162 commented

C++ Legacy Application

Hi,

I have this legacy C++ application, compiled using VS 2015. The application has been installed on a number of sites where it functions as expected. It has been installed on a site in Libya where it has severe issues:

1) On startup, the app loads a CArchive file which produces a "Out of memory" error. This file is < 2Kb. If I delete this file, the app will load fully.

2) There are a number of other definition files that are also CArchive files. I can select a file, and click "Ok" and I get the message "Failed to open document".

I can get around these issues by creating the files on the target system although they may not be fully representative to be of any practical use.

3) I need to select a data source used by the application. The list of supported external devices is in a pull-down list. The list is displayed correctly, I can select an option but when I click "Ok" I get the error

"The parameter is incorrect".

The target machine is 64bit WIndows 7 4Gb memory. The application is 32-bit C++ app.

I have tried the app on 3 systems in Libya, one being WIn 10 64 bit, and each one gives the "Out of memory".

I'm at my wits end on this issue, if someone can offer any advice, it would be most appreciated.

Many thanks

c++
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@IDH-0162

A CArchive object provides a type-safe buffering mechanism for writing or reading serializable objects to or from a CFile object. Usually the CFile object represents a disk file; however, it can also be a memory file (CSharedFile object).

You may save CArchive in memory instead of a file. I suggest you could try it in 64-bit 8GB RAM system (you should rebuild for a 64 bit target).

And I suggest you could try catching std::bad_alloc exception.


0 Votes 0 ·

Hi
Many thanks for your response. Rebuilding is impractical for us at the moment. My app has ben shown to run on systems with 2Gb (albeit sluggish) and 4Gb. I just can't see why there should be any issue. I am having to test any theories over the internet.

0 Votes 0 ·

Hi MiaWuMSFT,
Thank-you for your offer of assistance. You ask for the source files of my project. I would like to add that the application uses charting and serial third party software. Will the source code be sufficient for your needs?

Regards

0 Votes 0 ·

you might have version mismatch. that is, your archived file is from an earlier version of the program. The author is supposed to bump the version number in IMPLEMENT_SERIAL when the data format changes, call CArchive::GetObjectSchema in the Serialize function if there are multiple versions of the data, and throw CArchiveException::badSchema when it encounters a version that it doesn't understand. Reading blindly without version checking could set a string's buffer length to a rather large unit that your memory can't handle.

0 Votes 0 ·

1 Answer

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered IDH-0162 commented

Hi

1,Firstly, the “Out of memory” error is caused by ‘CMemoryException’ thrown at:

 ReadWeatherStationFile()
    
                   --> m_AWS.Serialize(ar)
    
                        --> CObList::Serialize(CArchive& ar)
    
                             --> ar >> newData -- _AFX_INLINE CArchive& AFXAPI operator>>(CArchive& ar, CObject*& pOb){ pOb = ar.ReadObject(NULL); return ar; }
    
                                  --> CArchive::ReadObject(const CRuntimeClass* pClassRefRequested)
    
                                        --> pOb = pClassRef ->CreateObject() //Fail to CreateObject here, pOb is null, so AfxThroMemoryException() is called.

2,Next, go further to investigate why “CreateObject()” is failed. In the Constructor of ‘WeatherStation’, CTime(1970,1,1,0,0,0) brings the original error. For CTime function itself, it’s OK to pass 1970,1,1,0,0,0 since the lower limit for the function is 1/1/1970 12:00:00 AM GMT, any time ranging from midnight, January 1, 1970 through December 31, 3000 Universal Coordinated Time (UTC) is OK. Whilst, CTime will call _mktime64, it is impacted by different time zones.

After an adjustment to UTC, _mktime32 handles dates from midnight, January 1, 1970, to 23:59:59 January 18, 2038, UTC. mktime64 handles dates from midnight, January 1, 1970 to 23:59:59, December 31, 3000. This adjustment may cause these functions to return -1 (cast to time_t, time32_t or _time64_t) even though the date you specify is within range. For example, if you are in Cairo, Egypt, which is two hours ahead of UTC, two hours will first be subtracted from the date you specify in timeptr; this may now put your date out of range.

3, Thus, you should determine the parameters of CTime by considering the time difference. For example, my time zone is UTC+8h, so I should call CTime(1970,1,1,8,0,0,0).

72992-314.png


Modify the parameters of CTime by considering the time difference. For example, my time zone is UTC+8h, so I should call CTime(1970,1,1,8,0,0,0).

Then the m_time value returned by _mktime64 would be 0 rather than -1, the error would disappear, CreateObject() would success and then the Xyrix application can run successfully!

72820-315.png

72920-316.png

72944-317.png

I suggest you could also try to use CTime(1970,1,2,0,0,0,0), and then deal with the time difference according to the time zone later.

Best Regards,

Jeanine




If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




314.png (177.4 KiB)
315.png (35.7 KiB)
316.png (61.1 KiB)
317.png (6.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Many thanks for your analysis on the Out of Memory issue. I will go though the issues you have raised.

0 Votes 0 ·

Hi Jeanine,

I would like to thank you again for your valuable input. I have confirmed your analysis of the issue and will take the necessary precautions in the code.

0 Votes 0 ·