| | |
Any Ideas....
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
I am just posting in the hopes that somenone can explain a problem I seem to be having with Turbo C++. My application basically has two forms (Form1 and Form2). Form1 is the main form and Form2 is created dynamically, as the result of a button click event, from Form1 with the following code:
Form2 is not included in the Auto_Create forms list under Project Options. Form2 accesses a thread object, derived from the TThread class, called Thread. A new instance of the thread is created with the following code:
This is set running following a button click event with the following code:
The thread updates the contents of a TMemo object named Memo1 located on Form2. The thread code looks somthing like this:
The UpdateMemo function is defined in the Thread header file as:
For some reason this code generates an access violation and I can't figure out why. If I include Form2 in the Auto_Create forms list under Project Options the thread runs (without generating errors), but doesn't update Memo1!! If I compile Form2 on its own or include Form2 in the Auto_Create forms list under Project Options and display it using the code:
without creating it dynamically, the thread runs and updates Memo1 as it should. I am guessing therefore that the problem arises from the way that Form2 has been created. Any help would be much appreciated as I am really quite stumped!
Many thanks in advance.
Ben.
C++ Syntax (Toggle Plain Text)
TForm2 *Form2 = new TForm2(this); Form2–>ShowModal();
Form2 is not included in the Auto_Create forms list under Project Options. Form2 accesses a thread object, derived from the TThread class, called Thread. A new instance of the thread is created with the following code:
C++ Syntax (Toggle Plain Text)
Thread *Thread1 = new Thread(true);
This is set running following a button click event with the following code:
C++ Syntax (Toggle Plain Text)
Thread1->Resume();
The thread updates the contents of a TMemo object named Memo1 located on Form2. The thread code looks somthing like this:
C++ Syntax (Toggle Plain Text)
void __fastcall Thread::Execute() { Synchronize(&UpdateMemo) } //--------------------------------------------------------------------------- void __fastcall Thread::UpdateMemo(void); { Form2->Memo1->Lines->Add("Thread Running!"); } //---------------------------------------------------------------------------
The UpdateMemo function is defined in the Thread header file as:
C++ Syntax (Toggle Plain Text)
void __fastcall Thread::UpdateMemo(void);
For some reason this code generates an access violation and I can't figure out why. If I include Form2 in the Auto_Create forms list under Project Options the thread runs (without generating errors), but doesn't update Memo1!! If I compile Form2 on its own or include Form2 in the Auto_Create forms list under Project Options and display it using the code:
C++ Syntax (Toggle Plain Text)
Form2–>ShowModal();
without creating it dynamically, the thread runs and updates Memo1 as it should. I am guessing therefore that the problem arises from the way that Form2 has been created. Any help would be much appreciated as I am really quite stumped!
Many thanks in advance.
Ben.
Last edited by BAEdwards; May 20th, 2008 at 6:19 am. Reason: Problem clarified
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Interesting.
Do you have global and/or static variables anywhere ?
Also can you post:
- TForm2.h
- TForm2::TForm2() implementation
Unit1.h (Form1):
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { TForm2 *Form2 = new TForm2(this); Form2->ShowModal(); } //---------------------------------------------------------------------------
Unit2.h (Form2):
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #ifndef Unit2H #define Unit2H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ComCtrls.hpp> //--------------------------------------------------------------------------- class TForm2 : public TForm { __published: // IDE-managed Components TButton *Button1; TMemo *Memo1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm2(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm2 *Form2; //--------------------------------------------------------------------------- #endif
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit2.h" #include "Unit3.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2 *Form2; Thread *Thread1 = new Thread(true); //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { Thread1->Resume(); } //---------------------------------------------------------------------------
Unit3.h (Thread):
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #ifndef Unit3H #define Unit3H //--------------------------------------------------------------------------- #include <Classes.hpp> //--------------------------------------------------------------------------- class Thread : public TThread { private: protected: void __fastcall Execute(); public: __fastcall Thread(bool CreateSuspended); void __fastcall Thread::UpdateMemo(void); }; //--------------------------------------------------------------------------- #endif
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit3.h" #include "Unit2.h" #pragma package(smart_init) //--------------------------------------------------------------------------- __fastcall Thread::Thread(bool CreateSuspended) : TThread(CreateSuspended) { } //--------------------------------------------------------------------------- void __fastcall Thread::Execute() { for (int i = 0; i <= 1000; i++) { Synchronize(&UpdateMemo); } } //--------------------------------------------------------------------------- void __fastcall Thread::UpdateMemo(void) { Form2->Memo1->Lines->Add("Thread Running"); } //---------------------------------------------------------------------------
Thanks,
Ben.
Couldn't figure anything from the code, given that it's not complete.
- How does Thread get Form2 obj?
- The access violation could be because one of the variables (Form2 or Memo1 or Lines) is uninitialized.
But can't figure out any of these from the code.
Just a couplea other thoughts:
- Check the owner's value when TForm2::TForm2(TComponent* Owner) gets called. Print out some instance specific info of TComponent that's passed. May it's different when "framework" creates it (instead of when you create it viz Form1 object)
- Other thing that I suspect more is, Memo, is an "// IDE-managed Components", so perhaps when IDE creates the Form2 (via your project settings) it takes care of init'ing it somehow?
- How does Thread get Form2 obj?
- The access violation could be because one of the variables (Form2 or Memo1 or Lines) is uninitialized.
But can't figure out any of these from the code.
Just a couplea other thoughts:
- Check the owner's value when TForm2::TForm2(TComponent* Owner) gets called. Print out some instance specific info of TComponent that's passed. May it's different when "framework" creates it (instead of when you create it viz Form1 object)
- Other thing that I suspect more is, Memo, is an "// IDE-managed Components", so perhaps when IDE creates the Form2 (via your project settings) it takes care of init'ing it somehow?
Are you Agile.. ?
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
Many thanks for your reply. I am afraid I am a reasonably new to C++ so you will have to excuse my ignorance.
I believe thread can locate the Form2 object by means of the include statement at the top of Unit3.cpp...
As to the initialisation state of Form2 and Memo I am not sure how I would go about checking this. Any direction would be much appreciated. I assumed that as objects are created on the form at design time, they would initialised in exactly the same way irrespective of whether the form they are associated with is dynamically created or automatically created when the program runs. Is this not the case?
How would I go about checking the initialisation state of Form2 and Memo1 etc so I can determine whether any difference arises as the result of the different ways of creating the form? Any further help would be much appreciated.
Many thanks,
Ben.
BTW: Although the code posted is not from the project I am working on, it is complete and should compile and replicate the problem I am experiencing.
I believe thread can locate the Form2 object by means of the include statement at the top of Unit3.cpp...
C++ Syntax (Toggle Plain Text)
#include "Unit2.h"
How would I go about checking the initialisation state of Form2 and Memo1 etc so I can determine whether any difference arises as the result of the different ways of creating the form? Any further help would be much appreciated.
Many thanks,
Ben.
BTW: Although the code posted is not from the project I am working on, it is complete and should compile and replicate the problem I am experiencing.
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
I think I've cracked it!! If I create Form2 using the following code
Instead of
the thread is now able to access the TMemo component on Form2. I'm not entirely sure why this works so if anyone has a couple of minutes spare and can offer me an explaination it would be much appreciated.
Thanks for taking the time to look at my code and offering some thoughts thekashyap . Cheers,
Ben.
C++ Syntax (Toggle Plain Text)
Form2 = new TForm2(this); Form2->ShowModal();
Instead of
C++ Syntax (Toggle Plain Text)
TForm2 *Form2 = new TForm2(this); Form2->ShowModal();
the thread is now able to access the TMemo component on Form2. I'm not entirely sure why this works so if anyone has a couple of minutes spare and can offer me an explaination it would be much appreciated.
Thanks for taking the time to look at my code and offering some thoughts thekashyap . Cheers,
Ben.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
// Your thread accesses the form via a variable named 'Form2'
// The 'Form2' variable below is local to the function and hence
// hides the intended 'Form2' variable i.e. the correct 'Form2' does
// not get assigned to anything.
TForm2 *Form2 = new TForm2(this);
Form2->ShowModal(); He he.. 
Stupid bug..
mitmkar has already explained it. Check out http://www.functionx.com/cppcli/variables/Lesson22.htm if still not clear.
PS: To catch such bugs in future, check the compiler option to increase the warning level. In Sun workshop it was +w2. RTM.
Better way is to use lint (http://www.splint.org/win32.html) though.
Totally unrelated to the problem cheat sheet for Turbo C++ found while looking for compiler guide.. http://www.sff.net/people/brian-lars...0turbo%20c.htm

Stupid bug..
mitmkar has already explained it. Check out http://www.functionx.com/cppcli/variables/Lesson22.htm if still not clear.
PS: To catch such bugs in future, check the compiler option to increase the warning level. In Sun workshop it was +w2. RTM.
Better way is to use lint (http://www.splint.org/win32.html) though.
Totally unrelated to the problem cheat sheet for Turbo C++ found while looking for compiler guide.. http://www.sff.net/people/brian-lars...0turbo%20c.htm
Are you Agile.. ?
•
•
•
•
As to the initialisation state of Form2 and Memo I am not sure how I would go about checking this. I assumed that as objects are created on the form at design time, they would initialised in exactly the same way irrespective of whether the form they are associated with is dynamically created or automatically created when the program runs. Is this not the case?
How would I go about checking the initialisation state of Form2 and Memo1 etc so I can determine whether any difference arises as the result of the different ways of creating the form? Any further help would be much appreciated.
Also set it back to NULL once the object to which points to is deleted. Usually d'tor.
If you always ensure that you do this, you can always check anywhere in the code: if your pointer is NULL then it's not initialized correctly, else it's usable.
If you don't initialize it the problem is it'll contain junk value and if you're really unlucky it might even work !
Are you Agile.. ?
![]() |
Similar Threads
- Any ideas make my forums grow? (Website Reviews)
- Ideas for a new XT compatible OS... (IT Professionals' Lounge)
- Need Ideas For Dissertations, Final Year Degree Project (IT Professionals' Lounge)
- Wow...lots wrong with my computer!!! Please look if you have ANY ideas w/o reformatti (Windows NT / 2000 / XP)
- Any Ideas for Projects???? (*nix Software)
- Group Project Ideas (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: what else cout<<??
- Next Thread: just in case you wanted to know
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





