Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DEBUG mode working, RELEASE mode not working

Status
Not open for further replies.

stream2000

Programmer
Aug 17, 2002
6
US
I have a problem with this part of my program. In debug mode it's working but in release mode The CString value(m_sTime) is going nutts. I'm very confused.
What I'm trying to do is some kind of a time bomb.I have the time set in seconds(member variable long m_iTime). The timer sends a message at one second and I increase it in (long m_iCount).And I'm going to write the time remaining in a CStatic Object (CString m_sTime).(HH:MM:SS)
There can't be given more then 99 hours or 99 minutes or 99 seconds.
ex : (10:34:02)(10:34:01)(10:34:00)(10:33:59).....



void CProgramDlg::OnTimer(UINT nIDEvent)
{

m_iCount++;

long iTime01=0;
long iTime02=0;
long iTime03=0;

char time1[2]="";
char time2[2]="";
char time3[2]="";

iTime01=(m_iTime-m_iCount)/3600;
iTime02=((m_iTime-m_iCount)%3600)/60;
iTime03=((m_iTime-m_iCount)%3600)%60;
if(iTime01!=100)
{
ltoa(iTime01 , time1 , 10);
}
ltoa(iTime02 , time2 , 10);
ltoa(iTime03 , time3 , 10);



if(iTime01/10==0)
{
m_sTime+="0";
m_sTime+=time1;
}
else
{
m_sTime+=time1;
}


m_sTime+=":";


if(iTime02/10==0)
{
m_sTime+="0";
m_sTime+=time2;
}
else
{
m_sTime+=time2;
}

m_sTime+=":";

if(iTime03/10==0)
{
m_sTime+="0";
m_sTime+=time3;
}
else
{
m_sTime+=time3;
}

UpdateData(FALSE);

CTrayDialog::OnTimer(nIDEvent);
}
 
You need to initialize [tt]m_sTime[/tt] before you try to add stuff onto it:

[tt]CString m_sTime = "";[/tt]

Because it appears to be a class member (no sign of initialization in your sample function) I'm not sure if you've already done this in your classes constructor. If not, then this will definitely be your problem because you are

[tt]m_sTime += something[/tt] if not initialized beforehand - what data is the 'something' being added to???

String initialization is a classic for causing release builds to fail when the debug version will work. For some strange reason, debug version appears to 'initialize' the strings for you!

:)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Thanks for the tip but the variable was already initialized
CString m_sTime=_T("");
Can you tell me what else can be wrong except this?
And by the way where does this _T("") initialization come from, is this better?
 
the data type mapping shouldn't make any difference (_T) so long as the variable is initialized. You may find that it is some other variable or pointer that has not been initialized which is causing the problem.
Look through your code and see if you can spot any pointers that have not been initialized to NULL before you try to use them. Also, ensure that all other variables are initialized to at least some value or another!

I had the exact same problem with a project where I had forgotten to initialize a simple counter:

[tt]
int read; // <- not initialized!!

while(read>0) // <- what value is 'read'?????
{
// .. do something
}[/tt]

worked fine in debug mode but not release mode - funnily enough, this problem drove me insane for almost a full day before I spotted it!!!

:)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
It seems from your post that the line
CString m_sTime=_T(&quot;&quot;);
is in your .h file. I don't think you can initialize member variables when you declare them, but you should rather do it in the class' constructor.

If i am right, then m_sTime is NOT initialized, and there is your problem.

If I am wrong... then tell me, and I'll learn something new :)

Side note: when you allocate memory (say with &quot;new&quot;) in debug mode, the memory is initialized to 0s. But in release mode, you get whatever garbage was in that memory location before you got it. That is why uninitialized variables can cause problems in release mode only.

Vincent
 
[tt]CString m_sTime=_T(&quot;&quot;);[/tt] can't believe I missed that!! Vince is right, all class member variables (except statics) need to be initialized in the constructor - not the class definition itself - eg, this is WRONG!:

[tt]class MyClass
{
public:
MyClass(){}
virtual ~MyClass(){}

private:
CString m_sTime=_T(&quot;&quot;);
};[/tt]

This is correct:

[tt]class MyClass
{
public:
MyClass(){ m_sTime=_T(&quot;&quot;); }
virtual ~MyClass(){}

private:
CString m_sTime;
};[/tt]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top