GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Nov-2006, 04:33
Gix Gix is offline
New Member
 
Join Date: Sep 2006
Posts: 21
Gix is on a distinguished road

Thread Stopping Exception


Hi,

I am making this server program that collects info from PCs from around a network when they are active.

After having many problems solved by the help of you guys I am trying now with my biggest problem...

OK. I have a thread:

CPP / C++ / C Code:
//calling a thread to wait for packets from client programs
Thread *readThread = new Thread(new ThreadStart (this, WaitForPackets));
readThread->Start();

and in my function WaitForPackets I have this part of a code:

CPP / C++ / C Code:
PC *Comps[];

PC *Comp = new PC(lblLab->Text, lblHost->Text, lblUser->Text, DateTime::Now, cycle);
					
Comps[i] = Comp;

Basically, I have made a PC class (I can post the code of this one too if needed but I think it is not) and I want to put every new instance of a PC in an array of PCs so that I can refer to if needed in the program.

My Unhandled exception (Thread is being stopped) occurs when I create the PC* Comp = new...

Can you please give me an idea of how to correct this one, and / or if you think I can do this another way (not with array of objects etc.)?

Thanks a lot guys you've been very helpful,

Gix
  #2  
Old 26-Nov-2006, 08:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Thread Stopping Exception


Quote:
Originally Posted by Gix
I have this part of a code:

CPP / C++ / C Code:
PC *Comps[];

This is invalid. It looks like you are trying to declare an array of pointers to PC, but you didn't give the size of the array. (No compiler should accept this.)

If you want an array of pointers, consider the following. I have made my own simple class for purposes of illustration.

CPP / C++ / C Code:
#include <iostream>

using namespace std;

class PC
{
  private:
    int k;

  public:
    PC(){ k = 0;}
    PC(int n) {k = n;}
    int getk(void) { return k; }
};


int main()
{
    PC **Comps; // will be an array of n pointers to PC
    int size;
    int i;


    cout << "How many PC objects will you put into the array: ";
    cin >> size;

    if (cin.fail() || (size <= 0)) {
        cout << "You must enter a positive integer." << endl;
        return 0;
    }
    

    Comps = new PC *[size]; // allocate memory for the array of pointers

    for (i = 0; i < size; i++) { // allocate memory and initialize each element
        Comps[i] = new PC(i+42);
    }

    for (i = 0; i < size; i++) {
        cout << "Comps[" << i << "]->getk() = " 
             <<  Comps[i]->getk() << endl;
    }
    for (i = 0; i < size; i++) {
        delete Comps[i];
    }

    delete [] Comps;
    return 0;
}
Note that you don't really need an array of pointers, unless there is some particular reason that you want Comps[i] to be a pointer. You could do it with an array of PC objects. (Declare Comps to be a pointer to PC and allocate enough memory for the array, then allocate and initialize and copy each element into the array.)

CPP / C++ / C Code:
.
.
.
    PC *Comps = new PC[size];    // allocate storage for the array of objects

    for (i = 0; i < size; i++) { // now allocate and initialize the elements
        PC *Comp = new PC(i+42); // allocate memory for and initialize a new object
        Comps[i] = *Comp;        // Copy the new object into the array
    }

    for (i = 0; i < size; i++) {
        cout << "Comps[" << i << "].getk() = " 
             <<  Comps[i].getk() << endl;
    }
.
.
.


Identical output.



Regards,

Dave
Last edited by davekw7x : 26-Nov-2006 at 09:33.
  #3  
Old 26-Nov-2006, 10:42
Gix Gix is offline
New Member
 
Join Date: Sep 2006
Posts: 21
Gix is on a distinguished road

Re: Thread Stopping Exception


Thanks for the help, but when I initialize the array like this:

CPP / C++ / C Code:
PC *Comps = new PC[200];

I get this errors:
error C2691: 'PC' : invalid type for __gc array element

error C3149: 'PC' : illegal use of managed type 'PC'; did you forget a '*'?

error C2691: 'PC __gc *' : invalid type for __gc array element

error C2845: '[' : cannot perform pointer arithmetic on __gc pointer 'PC __gc *'


That's why I was doing it the other way:

CPP / C++ / C Code:
PC *Comps[200];

But my question is that I get an exception in the code... that the thread called is being stopped each time I create the PC object, thus not letting the other part of the code to run properly...

Can you please check this, the code is in my first post?

Thank you,

Gix
  #4  
Old 26-Nov-2006, 12:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Thread Stopping Exception


Quote:
Originally Posted by Gix
when I initialize the array like this:

CPP / C++ / C Code:
PC *Comps = new PC[200];


1. Your original post in this thread didn't indicate that PC was a __gc class.

2. Your original post had this:PC *Comps[];, not PC *Comps[200];.

3. I still stand by my opinion that your statement PC *Comps[]; should be invalid. Apparently the compiler takes it to mean PC **Comps, which is not the same thing at all. If that is what the compiler means, then the value is not initialized and the exception is caused when your assignment dereferences it.

That is, if **Comps is equal to zero, then Comps[i] is illegal, since it tries to get someting from address zero. (I believe that the exception was caused by the assignment, not the instantiation.)



You could save some time if you make absolutely clear what you are working on. (The fact that a previous thread was about __gc classes doesn't mean that everyone understands what this one is about. Not everyone even read the old thread. I read the old thread, but did not understand that this one was about __gc classes. Other things coming into my mind crowd out the older things. Even if I had remembered your previous question, I would not automatically assume that you were talking about the same data type.)


Quote:
Originally Posted by Gix
But my question is that I get an exception in the code... that the thread called is being stopped each time I create the PC object, thus not letting the other part of the code to run properly...
.
.
Can you please check this, the code is in my first post?
Try this:

CPP / C++ / C Code:
   PC *Comps[] = new PC *[200]; // an array of 200 pointers to PC objects
.
.
.
    Comps[i] = new PC(//whatever);
.
.
.

Back to my example: One thing you are right about, __gc objects are pointers, so an array of __gc objects is an array of pointers.

CPP / C++ / C Code:
// test__gc.cpp
// compile with: cl /clr:oldSyntax test__gc.cpp
#include <iostream>
using std::cout;
using std::endl;

#using <mscorlib.dll>
using namespace System;

__gc class X {
    private:
       int i;
    public:
       X(){i = 0;}
       X(int n) {i = n;}
       int getI() {return i;}
};

int main() {
   int j;
   // try it the way that you had it in your post
   X *zzz[];
   cout << "zzz = " << zzz << endl << endl;
   // note that since zzz is equal to zero zzz[i] would be illegal
   //
   //
   X *Comps[] = new X *[200]; // an array of 200 pointers to X objects
   for (j = 0; j < 5; j++) {
       X *Comp = new X(j+42);
       Comps[j] = Comp;
   }

   for (j = 0; j < 5; j++) {
       cout << "Comps[" << j << "]->getI() = " << Comps[j]->getI() << endl;
   }
   return 0;
}


Output:
Code:
zzz = 0 Comps[0]->getI() = 42 Comps[1]->getI() = 43 Comps[2]->getI() = 44 Comps[3]->getI() = 45 Comps[4]->getI() = 46

You don't need a separate object Comp, you could allocate and assign in one step:

CPP / C++ / C Code:
   for (j = 0; j < 5; j++) {
       Comps[j] = new X(j+42);
   }

Regards,

Dave
  #5  
Old 26-Nov-2006, 12:39
Gix Gix is offline
New Member
 
Join Date: Sep 2006
Posts: 21
Gix is on a distinguished road

Re: Thread Stopping Exception


OK. Sorry for not being clear in the first place.

Second, yes... I am talking about a __gc class, and again sorry for not mentioning this ( I take some things for granted, my bad )

Now let me answer your questions so that you can further help me if you can.

1. Yes. PC is a __gc class.

2. I am still initializing the array like this:
CPP / C++ / C Code:
PC *Comps[];

like in your example:
Quote:
CPP / C++ / C Code:
X *zzz[];

I do this because it is the only way the compiler accepts it.

3. OK here I'll try to make my question clearer...

This is my thread:
CPP / C++ / C Code:
//calling a thread to wait for packets from client programs
Thread *readThread = new Thread(new ThreadStart (this, WaitForPackets));
readThread->Start();

Now in my WaitForPackets function that it calls upon I have this line of code that generates the exception during runtime:

CPP / C++ / C Code:
PC *Comp = new PC(lblLab->Text, lblHost->Text, lblUser->Text, DateTime::Now, cycle);

so it is not the array that is the problem that causes the exception, but it is the creation of this new object of the __gc class PC.

I hope now I got it clearer.

Thank you for trying so hard to help me again,

Gix
  #6  
Old 26-Nov-2006, 13:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Thread Stopping Exception


Quote:
Originally Posted by Gix
OK. Sorry for not being clear in the first place.

Second, yes... I am talking about a __gc class, and again sorry for not mentioning this

Well, I didn't mean to sound snippy. Sometimes my responses sound a little more curt than I really mean them to be.

Quote:
Originally Posted by Gix
Now let me answer your questions so that you can further help me if you can.

1. Yes. PC is a __gc class.

2. I am still initializing the array like this:
.
.

I do this because it is the only way the compiler accepts it.


Put the following statement in your program and tell me what it says:

CPP / C++ / C Code:
    PC *Comps[];
    cout << "Comps = " << Comps << endl << endl;
 

If it says Comps = 0 then it won't work

Did you try
CPP / C++ / C Code:
   PC *Comps[] = new PC *[200]; // an array of 200 pointers to PC objects

We are using different versions of compilers (and it is possible that the bugs in yours are different from the bugs in mine), and If your compiler won't accept this, then I have reached the end of our journey together, if not the end of the road altogether. That's just about the only way that I can think of to get an array of __gc objects.

Actually I guess that you can separate the statements like this:

CPP / C++ / C Code:
  PC *Comps[];
  Comps = new PC *[200];

But without actually allocating memory for the array, I don't see how you can ever expect to use it (regardless of other problems with the code).


Quote:
Originally Posted by Gix

Now in my WaitForPackets function that it calls upon I have this line of code that generates the exception during runtime:

CPP / C++ / C Code:
PC *Comp = new PC(lblLab->Text, lblHost->Text, lblUser->Text, DateTime::Now, cycle);


How do you know? (I am not challenging your assertion; I just want to know.) Did you step through with a debugger? Did you put print statements just before and just after the object instantiation? Or What?

Whatever method you used to track down the bad behavior to this point: use the same method to go farther into it.


Since I have no idea what the constructor is doing with its arguments and I have no idea what the argument values are, I am at a loss. (I doubt that I would be able to get more out of additional code, since there is, obviously, a lot of it.)

Depending on what you have done so far, maybe you can print out the values of aguments, or at least make sure that the program is working on what you think it should be working on. Or maybe you can go into the constructor and put debug statements to print out the progress through the statements there. (Or, better yet, if you can step through the code with a debugger, you can find the actual place that causes the exception and see what's happening at that exact place in the code.)

Regards,

Dave
  #7  
Old 26-Nov-2006, 13:27
Gix Gix is offline
New Member
 
Join Date: Sep 2006
Posts: 21
Gix is on a distinguished road

Re: Thread Stopping Exception


Quote:
Originally Posted by davekw7x
Well, I didn't mean to sound snippy. Sometimes my responses sound a little more curt than I really mean them to be.

No problem.

Quote:
Originally Posted by davekw7x
But without actually allocating memory for the array, I don't see how you can ever expect to use it (regardless of other problems with the code).

You are correct here... I didn't really think about this when writing my code and I corrected it now.

Now about my REAL tough problem!

I know that the pointed line causes the exception because I run it with a debugger and I put breakpoints through the code... The program crashes when it reaches the creation of the PC Object... It doesn't even enter inside the constructor of the class, but dialogs out the following message:

Quote:
An unhandled exception of type 'System.Threading.ThreadStopException' occurred in mscorlib.dll

Additional information: Thread was being stopped.

Then I hit Break or Continue and that's it... none of the code was executed from there on...
  #8  
Old 26-Nov-2006, 15:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Thread Stopping Exception


Quote:
Originally Posted by Gix
I know that the pointed line causes the exception because I run it with a debugger and I put breakpoints through the code... The program crashes when it reaches the creation of the PC Object... It doesn't even enter inside the constructor of the class, but dialogs out the following message:

My final desperate suggestion: Have you tried running it as Release, not Debug (put print statements before and after the creation line).

Even having your complete code probably wouldn't help, since I don't have your environment.

Sorry.

Regards,

Dave
Last edited by davekw7x : 26-Nov-2006 at 16:24.
  #9  
Old 26-Nov-2006, 16:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Thread Stopping Exception


Quote:
Originally Posted by davekw7x
My final desperate suggestion
Well, one more comment:

If it's not a language thing (and I'm beginning to think it is not), then it's an application thing.

Do your child threads try to change the User Interface? Could that be a problem? (Have you run anything like this before? In Debug mode?)

Regards,

Dave
  #10  
Old 27-Nov-2006, 08:54
Gix Gix is offline
New Member
 
Join Date: Sep 2006
Posts: 21
Gix is on a distinguished road

Re: Thread Stopping Exception


DISREGARD THIS POST... SORRY FOR DOUBLE POST


Hi again...

There has been a development in this problem of mine... So now I run the program (debug mode) and instead of usting the previous constructor I use this:

CPP / C++ / C Code:
PC *Computer = new PC();

The default constructor. And there is no exception and everything runs smoothly, the exception happens when I use the other constructor...

Now I am going to paste the class PC here, can you please tell me if you see anything wrong in the constructor PC(String* BT, String*, .... ..., int AN):


CPP / C++ / C Code:
#pragma once

using namespace System;

public __gc class PC
{
public:
	PC();
	PC(String* BT, String* HN, String* AU, DateTime LU, int AN);
	PC(PC *Comp);

	System::String __gc* ToString();
	void static op_Assign(PC *Cm, PC *Comp);
		
private:
	String* Lab;
	String* HostName;
	String* ActiveUser;
	DateTime LastUpdate;
	int AckNumber;

}; // end class PC definition


// constructors
PC::PC() {}

PC::PC(String* BT, String* HN, String* AU, DateTime LU, int AN)
{
	Lab = BT;
	HostName = HN;
	ActiveUser = AU;
	LastUpdate = LU;
	AckNumber = AN;
}

PC::PC(PC *Comp)
{
	Lab = Comp->Lab;
	HostName = Comp->HostName;
	ActiveUser = Comp->ActiveUser;
	LastUpdate = Comp->LastUpdate;
	AckNumber = Comp->AckNumber;
}

//functions
System::String  __gc* PC::ToString()
{
	return HostName;
}

void PC::op_Assign(PC *Cm, PC *Comp)
{
	Cm->Lab = Comp->Lab;
	Cm->HostName = Comp->HostName;
	Cm->ActiveUser = Comp->ActiveUser;
	Cm->LastUpdate = Comp->LastUpdate;
	Cm->AckNumber = Comp->AckNumber;
}

Thanks again,

Gix
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread itch C++ Forum 7 10-May-2006 15:10
please help in solving: Exception in thread "main" java.lang.nullpointerexception vkiran_v Java Forum 8 09-May-2006 07:06
C++ Copy Constructor & Exception issues greymalkin C++ Forum 3 05-Oct-2005 21:36
Sending message to GUI thread that ISNT the main thread mpviii MS Visual C++ / MFC Forum 1 20-Apr-2005 12:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:21.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.