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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple problems

Status
Not open for further replies.

marty13

Programmer
Jul 15, 2003
11
CA
Hiya all, i'm new around
I've been working with c/c++ for a while and now i'm trying visual c++ and i'm having some probs with my application (some simulation of production in real-time):

1. I'm trying to refresh the value of one field while i'm running my simulation but it wont work using UpdateData(FALSE) if it is in the center of the program (in the while)... if i use it when i begin or end, it's ok but not in the middle (and i really need to see the values to know if the program is doing ok).

2. I want do a emergency stop in case i want stop the program before it ends but when i'm starting my simulation, i cant access the main window and press on the button o_O

3.Even if i'm using #if !defined( ), #define and #endif in my headers, i cant use them with more than 1 object(1 objet includes another one that includes the header i need, and i include the header itself).

4.I an having some problems with malloc :

code:
--------------------------------------------------------------------------------

Nx1=1+(m_x1sup-m_x1inf)/m_hx1;Nx2=1+(m_x2sup-m_x2inf)/m_hx2;/*******************************cont1*********************************************/ if ((cont1=(float**)malloc(Nx1*sizeof(float*)))==NULL) { MessageBox(&quot;La memoire est insufisante pour l'allocation1.&quot;); exit(1); } for (i=0;i<Nx1;i++) if ((cont1=(float*)malloc(Nx2*sizeof(float)))==NULL) { MessageBox(&quot;La memoire est insufisante pour l'allocation.&quot;); exit(1); } initTab2d(&cont1[0], Nx1,Nx2);

--------------------------------------------------------------------------------

if nx1 is too big, it will stop at the second dimension somewhere BUT, if nx2 is the same size as nx1 (lets say 2000), it wont stop, and crash somewhere in the program.

I hope u can help me a little here, i have been searching for a while now =/
thx.
 
1) That's because your loop is blocking everything else from happening in your application.

Solution 1: Put a message pump inside to loop.

// Returns false if the application wants to be closed.
bool MessagePump()
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
PostQuitMessage(0);
return false;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}

Solution 2: Put the loop in another thread and have it PostThreadMessage to your window.

2) Pretty much same as above.

3) Are you perhaps using the same #if !defined( ) for the various files?

4) I never use malloc, only new. Anyway, you do a whole lot cont1 = malloc(somehting), but what happens to the previous cont1 = malloc( )?

I guess you try something like:

Code:
// Assuming cont1 is this
float** cont1;
  .
  .

cont1=new float*[Nx1]; 
// cont1 now has room for Nx1 pointers to floats
for (int i=0;i<Nx1;i++)
{
  cont1[i]= new float[Nx2];
  // cont1[i] now as room for Nx2 floats.

}

Later on you free the allocated data with
Code:
for (int i=0;i<Nx1;i++)
{
  delete [] cont1[i];
}
delete [] cont1;

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
------------------------------------
Solution 1: Put a message pump inside to loop.

// Returns false if the application wants to be closed.
bool MessagePump()
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
PostQuitMessage(0);
return false;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
--------------------------------
i see... i dont see what it does... do i just copy/paste it somewhere in the loop of my program?

------------------------
3) Are you perhaps using the same #if !defined( ) for the various files?
------------------------
well, the same commands but i define a name for each .h and when i'm trying to use fonction in a .h from many .cpp, it says it is defined many times (isnt the #if !defined() supposed to fix that?)

as for the malloc/new, its pretty much the same thing and it works pretty well... if the size can be allocated for both. Then, malloc should return NULL and the if SHOULD take care of this.
 
>> i see... i dont see what it does...

It processes windows messages from your applications message queue. The DispatchMessage() call will result in your UI being updated. Keep in mind that this will have an effect on the timing of your loop. To avoid that as much as possible, use a worker thread solution.

>> do i just copy/paste it somewhere in the loop of my program?

No, he posted a function MessagePump(). You make it a function in your application and call it from inside your loop.


-pete
 
>do i just copy/paste it somewhere in the loop of my program?

Yes. And if it returns false your loop should exit.

>isnt the #if !defined() supposed to fix that?
Indeed it is. Hard to say what is wrong without actually looking at the code. The #if !defined() should be the very first of the .h file, even before the #include.

>as for the malloc/new, its pretty much the same thing

Yes. new does everything malloc does and even more (calling constructors if any).

>and it works pretty well

So what's the problem then?




/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
-So what's the problem then?

if the number of elements is too big (i tryed with nx1=2000 and nx2=5), the message box appears and it works. If both are too big (tryed nx1=2000, nx2=2000), the message box, wont appear and the program will crash when i work with the arrays.
 
Ok. Could you try out my suggested solution before you dismiss it?


/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
the 1-2 (messagepump)is great :p (now i just have to figure out if i can pass my arrays to the exit fonction so it frees the memory :p)

as for the malloc/new sollution, i'll try it, but if it works, i'll have to change alot of things ­>.<

well, thx for your help =) i'll write about the results later.

 
PerFnurt
it seems to be working just fine now =)
The message pump is leading me to 2 more question... how can i hide my simulate button when i click on it? and how to convert a CString * to char * so i can open my file in the message pump fonction?
 
isnt there a edit button on this forum? :S
new problem with messagepump, the program is still running and i dont seem to be able to get the false in if(MessagePump()==FALSE) ... so i cant do a thing after this... how do i stop the program from the function itself and not make it run in background?
 
> how can i hide my simulate button
If it is an MFC application you typically map the control to a member veriable of the dialog class.

m_theButton.ShowWindow(SW_HIDE);

>when i click on it?
If it is an MFC application you just capture the clicks by letting the class wizard generate message map and an OnMyButtonClick() methods.

>CString * to char *
CString has an operator LPCTSTR() so the conversion is &quot;automatic&quot;. In a non-unicode application LPCTSTR == const char*

>open my file in the message pump fonction?
Hmmm...the message pump is (and should only be) a message pump. I would not recommend you oen files there.

>i dont seem to be able to get the false in if(MessagePump()==FALSE)
Your pump never gets the WM_QUIT message?
You could also check for other messages and have the pump return false on them to abort the loop's execution.

Btw, I would write if (!MessagePump()) instead of the == FALSE thingie. 1) It is unneccessary. 2) MessagePump returns a bool (true or false) not a BOOL (TRUE or FALSE). No big deal though, just a minor note.



/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
ok, to hide the button i used GetDlgItem(IDOK2)->EnableWindow(FALSE); and it works perfectly, but if i want to hide an item in the menu (lets say IDR_MENU1), i cant use that function, which one i use? EnableMenuItem? when i use it, it says it doesnt use 2 parameters o_O

>>i dont seem to be able to get the false in if(MessagePump()==FALSE)
>Your pump never gets the WM_QUIT message?
>You could also check for other messages and have the pump >return false on them to abort the loop's execution.
nope, it doesnt return WM_QUIT, neither does it return WM_DESTROY =/ I looked at the possibilities but i dont know what it could be.... for now, I have a quit button and his code is
void p_m1p2::OnCancel()
{
// TODO: Add extra cleanup here

CDialog::OnCancel();
}
and when it is clicked, i dont get a WM_QUIT, what message should it send me? then i could do the if on it and do my things....

thx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top