|
-
October 22nd, 2003, 09:00 AM
#1
pointer to pointer to heap
I'm not sure about this:
Does this work?
Code:
classA::classA(){
classA1 pa1 = new classA1;
classA2 pa2 = new classA2(pa1);
}
classA2::classA2(classA1* p){
m_pa1 = p; //Member-Variable
}
Later, I create a Thread that gets a pointer to m_pa1 and uses this pointer even after pa2 is destroyed.
threadFunctionBla(..){
classA2* pa2 = //get out of parameter//;
classA1* pa1 = pa2->m_pa1;
//do somwthing.
//classA2 is detroyed now by another Thread
pa1->doSomething(); //does this work? I get strange results.
i now it's confusing. if anybody sees any errors or wants to show a btter way, i'd be glad.
-
October 22nd, 2003, 09:44 AM
#2
Code:
classA2* pa2 = //get out of parameter//;
classA1* pa1 = pa2->m_pa1;
//do somwthing.
//classA2 is detroyed now by another Thread
pa1->doSomething(); //does this work? I get strange results
This is very hard to follow. You use the same names for the variables in the definition for ClassA and threadFunctionBla(..).
If i am following correctly, you seem to say that the pa2 variable in threadFunctionBla(..) really ends up pointing to an invalid address because of the statement:
//classA2 is detroyed now by another Thread
If this is the case, the call:
Code:
pa1->doSomething(); //does this work?
can't "work" properly, since pa2->m_pa1 is also now an invalid address. (classA1* pa1 = pa2->m_pa1)
Last edited by gjs368; October 22nd, 2003 at 09:55 AM.
-
October 22nd, 2003, 10:07 AM
#3
Use semaphores
A simple way to work around this is using semaphores, setting a global variable indicating if the object is already destroyed or not.
the following code is an exemple of this thecnic (not tested)
Code:
:
class A1
{
...
int * Semaphore;
A1(int * PSemaphore);
~A1();
...
}
A1::A1(int* PSemafhore):Semaphore(PSemaphore)
{
...
*Semaphore = 1 ; //--- Semaphore sinalizes if A1 is alive.
...
};
A1::~A1()
{
*Semaphore = 0; //--- A1 is destroyed....
}
class A2
{
...
int * A1Alive;
A2(int* PA1Alive); // on construction A1 recieves the semaphore
...
inline bool CheckA1Alive() { return *A1Alive == true;};
public:
void UseA1();
}
A2::A2(int * PA1Alive):A1Alive(PA1Alive){};
void A2::UseA1()
{
if (CheckA1Alive)
{
// work with A1...
}
else
{
// A1 is destroyed, nothing to do...
}
}
main()
{
int Semaphore = 0;
A1(&Semaphore);
...
A2(&Semaphore); // A2 needs to get ride of the semaphore
A2.UseA1();
...
:
-
October 22nd, 2003, 10:46 AM
#4
can't "work" properly, since pa2->m_pa1 is also now an invalid address.
Not neccessarily true.
Dereferencing pa2 will naturally fail, but:
Code:
classA1* pA1 = pA2->m_pA1;
delete pA2;
pA1->DoSomething();
can work fine, provided the destructor for classA2 doesn't explicitly delete m_pA1.
Regards,
Bassman
-
October 22nd, 2003, 10:54 AM
#5
thx bassmann.
no, classA2 does not delete m_pA1. But it may set
m_pA1 = 0;
Would this be a problem
-
October 22nd, 2003, 11:01 AM
#6
No, as long as you call
before
executes (obviously). Setting the pointer to 0 or even to another valid value won't delete the object that it points to.
Regards,
Bassman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|