|
good links luc... we replied almost simultaneously...
|
|
|
|
|
none at all? are you sure you get absolutely no errors? even if not specific, post them and that'll give us a clue as to the problem.
the debug software version does a few things differently so that the software doesn't crash immediately following an error, to allow for debugging. one such case is accessing an out of bounds location within an array, either to read or write... the release version would crash immediately, the debug version might not (will likely be caught with an ASSERT of some sort).
|
|
|
|
|
Thanks for your reply. I found the function that crashes the Release version.
I get the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
So it's either MyArray[50] that crashes it or the string builders.
<br />
void Read_Text()<br />
{<br />
char ch;<br />
int i;<br />
char MyArray[50];<br />
ifstream in("scores.ini", ios::in | ios::binary);
<br />
<br />
while(in && ch)
{
i++;<br />
in.get(ch);<br />
if(in) {MyArray[i-1] = ch;}<br />
}<br />
in.close();
<br />
StringBuilder^ sbA = gcnew StringBuilder(); <br />
int iA = 0;<br />
for(iA;MyArray[iA] != '_';iA++)
{<br />
sbA->Append((Char)MyArray[iA]);<br />
}<br />
String^ s = sbA->ToString();
score1 = Convert::ToInt32(s);<br />
<br />
StringBuilder^ sbB = gcnew StringBuilder(); <br />
int iB = iA+1;<br />
for(iB;MyArray[iB] != '_';iB++)
{<br />
sbB->Append((Char)MyArray[iB]);<br />
<br />
}<br />
String^ sB = sbB->ToString();
score2 = Convert::ToInt32(sB);<br />
<br />
StringBuilder^ sbC = gcnew StringBuilder(); <br />
int iC = iB+1;<br />
for(iC;MyArray[iC] != '_';iC++)
{<br />
sbC->Append((Char)MyArray[iC]);<br />
<br />
}<br />
String^ sC = sbC->ToString();
score3 = Convert::ToInt32(sC);<br />
<br />
StringBuilder^ sbD = gcnew StringBuilder(); <br />
int iD = iC+1;<br />
for(iC;MyArray[iD] != '_';iD++)
{<br />
sbD->Append((Char)MyArray[iD]);<br />
<br />
}<br />
String^ sD = sbD->ToString();
score4 = Convert::ToInt32(sD);<br />
<br />
<br />
if(show_msge==true){MessageBox::Show("Top Scores\n" + Convert::ToString(score1) + "\n" + Convert::ToString(score2) + "\n"+ Convert::ToString(score3) + "\n" + Convert::ToString(score4));}<br />
}<br />
|
|
|
|
|
wow, guess i'll start from the top:
- the initial statement while(in && ch) never has ch initialize
- index i never gets initialized
- are you checking for underscores? what happens if there's no underscore in your loop? runs until program breaks...
- you never check if any indexes will write out of bounds of the array
those are just some observations... and I didn't even bother going through it all because its so bad
|
|
|
|
|
Albert Holguin wrote: - index i never gets initialized
My bet is on that one.
I agree that there are many other problems, e. g. it should be just while (in) since ch==0 is a valid input. Also, why not use >> operators rather than get() ?
Besides, @OP, use the <pre> tags to format a block of code, not <code>.
|
|
|
|
|
don't know if I'd agree with using >> instead of get() , but there's a lot of problems in that code either way. i hope this is OP's homework and not work.
|
|
|
|
|
My point of using >> is that you can read a variable with just one command rather than put it together byte by byte. It might be difficult though to change the code, depending on the complexity of the data structure within that file. Also, the data in that file have to be stored in the right way, i. e. using << , to make sure they will be properly read using >> .
If all you ever use from ifstream is get() , then I have to ask, why use ifstream at all? That's like using MS Word for SMS...
|
|
|
|
|
>> is not type safe and reading incorrect input type leads to undefined behavior
|
|
|
|
|
Same with bytewise input: you just delay the problem from the reading part to the interpreting part. In either case you have to somehow validate the resulting values. Unfortunately this is almost impossible with binary inputs, and for that reason alone I'd retract my suggestion of using >> here.
|
|
|
|
|
a couple of other things... this is managed C++, belongs in the C++/CLI forum... what happens if your open file fails? the function will lead to a program crash since you have no error handling... i really hope this is just pseudo-code and not your real thing, if its your real thing, you may want to take a step back to the basics.
|
|
|
|
|
Its the real thing. Yea I know its bad but it works for what i'm doing. Suggestions on how to improve it are welcome.
|
|
|
|
|
As a rule of thumb, I found that most crashes that only happen in release versions are due to lack of intialization: In debug mode, all variables are initialized with 0 by default. In release mode, this is not the case. Specifically for pointers this can have disastrous consequences, e. g. when you test try to delete an object from a non-null pointer which presumably points to an object created by new.
The best way to avoid these type of errors is to always explicitely initialize a variable on the same line that declares them.
The best way to treat with these errors is locate the part of the code that causes it, and make sure every variable is properly initialized.
|
|
|
|
|
Well you guys were right about not initializing those variables. That fixed the problem. Thanks.
I'm not sure what's wrong with my code. How would you re-write it. I'm checking for under scores to check for the next score.
The file looks like this:
100_
200_
300_
400_
|
|
|
|
|
Wait, are you saying you're reading a standard ASCII text file? If so, why bother reading it as binary, byte by byte? Just open a normal file stream and use its standard operator >> to stream the numbers into your variables.
Try these functions to write and read your
void WriteScores(const std::vector<int>& scores) {
int nscores = (int) scores.size();
std::ofstream scorestream("Myscores.txt");
scorestream << nscores;
for (int i=0; i < nscores; ++i)
scorestream << ' ' << scores[i];
}
int ReadScores(std::vector<int>& scores) {
int nscores = 0;
std::ifstream scorestream("Myscores.txt");
scorestream >> nscores;
scores.resize(nscores);
for (int i=0; i < nscores; ++i)
scorestream >> scores[i];
return nscores;
}
You may want to add in some extra code to check for premature end of file or failure to create/write or read the file. You might also want to check the state of the filestream after reading each number to make sure an actual integer was read (if there was no integer, the resulting value will be 0, but since 0 may be a valid value, you can check on the state of your stream variable instead)
|
|
|
|
|
I've noticed that every new porject I create that utilizes the menu class from the MFC Feature Pack appears to utilize cleartype (or some form of font smoothing) regardless of my system settings.
I've looked for some way to disable it but so far have not found anything.
Does anyone know how to manipulate the settings for whether a CMFCMenuBar uses ClearType (or any type of font smoothing in general)?
UPDATE:
This might do the trick (Appears to work so far) and it appears to carry over to status bar, view tabs, etc...
void CMainFrame::DisableClearType(BOOL bDisable)
{
LOGFONT logFont = {0};
CFont& font = (CFont&)CMFCMenuBar::GetMenuFont();
font.GetLogFont(&logFont);
if (bDisable) {
logFont.lfQuality &= (~ANTIALIASED_QUALITY);
} else {
logFont.lfQuality |= ANTIALIASED_QUALITY;
}
CMFCMenuBar::SetMenuFont(&logFont);
}
void CMainFrame::OnApplicationLook(UINT id)
{
CWaitCursor wait;
theApp.m_nAppLook = id;
switch (theApp.m_nAppLook)
{
case ID_VIEW_APPLOOK_WIN_2000:
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManager));
DisableClearType(TRUE);
break;
case ID_VIEW_APPLOOK_OFF_XP:
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOfficeXP));
DisableClearType(FALSE);
break;
...
modified on Monday, April 11, 2011 3:53 PM
|
|
|
|
|
You can do it for the entire system, but I don't know of any way to do it for just one app. You might check the BCGSoft forums, they might have a secret API.
|
|
|
|
|
Hans Dietrich wrote: You can do it for the entire system, but I don't know of any way to do it for
just one app
The single MFC Feature Pack application is ignoring my system settings so I thought there might be a way to force it to behave and pay attention to the system settings but so far, I've dug around in the BCGSoft/Microsoft source code for CMFCVisualManager and the label/caption drawing code simply uses the DC passed in so I guess I need to see where that DC comes from and see if it can be modified.
I'll dig around the BCGSoft site. I have to say, by the time I figure out how to use this Feature Pack stuff, they'll have moved on to something new again.
|
|
|
|
|
Post a question on the BCGSoft forums.
|
|
|
|
|
Didn't have the secret API but something in the BCGSoft FAQ made a neuron fire.
Thanks for the assistance.
|
|
|
|
|
Hi All,
I have to hit a WebSocket server and have to maintain the connection alive.
Please share some article about implementing a WebSocket Client application in C.
Thanks All.
Do your Duty and Don't expect the Result
|
|
|
|
|
For a socket client in Windows you basically need to call the following functions in order -
WSAStartup[^] - Initialize winsock.
socket[^] - Create the socket specifying the required protocol like TCP, UDP etc.
connect[^] - Connect the previously created socket to the specified server.
After this step the connection is ready.
To communicate with the server you can use the following functions -
send[^]/sendto[^] - Send a stream of bytes to the server.
recv[^]/recvfrom[^] - Receive a stream of bytes from the server.
To tear down the connection the following functions must be called -
closesocket[^] - Disconnect
WSACleanup[^] - Cleanup winsock.
|
|
|
|
|
|
The WebSocket protocol works on top of a normal TCP socket connection, so you must understand Winsock first before you can start implementing WebSockets.
|
|
|
|
|
Thanks for the reponses.. guess i'm not clear with my question..
I'm clear about the WinSocket APIs.. just need to know the WebSocket Handshake details..
I got the below header from Wikipedia & i want to know what are all the things are variable values in this header?
GET /demo HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: example.com
Origin: http:
Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
^n:ds[4U
As far as i know the /demo in first line, Host & Origin will change.. but exactly what will be the values?
Will the values of Key1, Key2 & the last 8 byte values also change?
I have to hit the server which has the format as ws://xxx.xxx.xxx.xxx:8181/websock
Also is it possible to send any extra information in the above handshake header? if so what will be its format?
Thanks Again.
Do your Duty and Don't expect the Result
|
|
|
|
|