Access violation in a multithreaded application, C ++

I am not very good at multithreaded programming, so I would like to ask for help / advice.

In my application, I have two threads trying to access a shared object. You might think of two tasks that try to call functions from another object. For clarity, I will show you some parts of the program that may not be very relevant, but hopefully can help improve my problem.

Please see the sample code below:

//DataLinkLayer.h 
class DataLinkLayer: public iDataLinkLayer {

public:
DataLinkLayer(void);
~DataLinkLayer(void);
};

Where iDataLinkLayer is an interface (an abstract class without any implementation) that contains pure virtual functions and the declaration of a link (pointer) in the isntance of a DataLinkLayer (dataLinkLayer) object.

// DataLinkLayer.cpp
#include "DataLinkLayer.h"

DataLinkLayer::DataLinkLayer(void) {

/* In reality task constructors takes bunch of other parameters  
 but they are not relevant (I believe) at this stage. */
dll_task_1* task1 = new dll_task_1(this); 
dll_task_2* task2 = new dll_task_2(this); 

/* Start multithreading */
task1->start(); // task1 extends thread class
task2->start(); // task2 also extends thread class
}

/* sample stub functions for testing */
void DataLinkLayer::from_task_1() {
printf("Test data Task 1");
}

void DataLinkLayer::from_task_2() {
printf("Test data Task 2");
}

1 . DataLinLayer (iDataLinkLayer) cosntructor, dataLinkLayer isntance.

//data_task_1.cpp
#include "iDataLinkLayer.h"  // interface to DataLinkLayer
#include "data_task_1.h"

dll_task_1::dll_task_1(iDataLinkLayer* pDataLinkLayer) {
this->dataLinkLayer = pDataLinkLayer; // dataLinkLayer declared in dll_task_1.h
}

// Run method - executes the thread
void dll_task_1::run() {
// program reaches this point and prints the stuff
this->datalinkLayer->from_task_1();
}
// more stuff following - not relevant to the problem
...

2 simialrly:

//data_task_2.cpp
#include "iDataLinkLayer.h"  // interface to DataLinkLayer
#include "data_task_2.h"

dll_task_2::dll_task_2(iDataLinkLayer* pDataLinkLayer){
this->dataLinkLayer = pDataLinkLayer; // dataLinkLayer declared in dll_task_2.h
}

// // Run method - executes the thread
void dll_task_2::run() {
// ERROR: 'Access violation reading location 0xcdcdcdd9' is signalled at this point
this->datalinkLayer->from_task_2();
}
// more stuff following - not relevant to the problem
...

, (), . , , , , .

, - , dll_task_2 , DataLinkLayer. dll_task_2 , . , , , ... , , /.

Microsoft Visual ++ 2010 Express. , , , , .

+3
4

- 0xcdcdcdcd

Wikipedia :

CDCDCDCD Microsoft ++

MSDN.

free 0xddddddddd, , .

EDIT: , . , , ++ . ++ , , , , . , , ++ ( , v-) , ( , ctor-initializer-list ).

, :

-, , ++ : . , , , v- . this, , , , ( ) . this.

. , , (.. ), . ​​ ( - class task), , , , , , .

"!", : " task CreateThread, , ". , " ". task *, ? , , , intptr_t, task *, char *, as-if, task ( ). task, , this , task, this, .

. , , . . , , , , , Koz ( ), , , , , .

v-, task.

, " ".

+3

printf , afaik, . printf .

InitializeCriticalSection iDataLinkLayer. printfs EnterCriticalSection LeaveCriticalSection. printf.

: :

dll_task_1* task1 = new task(this); 
dll_task_2* task2 = new task(this);

to

dll_task_1* task1 = new dll_task_1(this); 
dll_task_2* task2 = new dll_task_2(this);

, dll_task_1 dll_task_2... , , , ....

+1

, 'this' (.. -) . , - DataLinkLayer DataLinkLayer. , - : http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7

0

DataLinkLayer.

DataLinkLayer main:

int main () {
DataLinkLayer* dataLinkLayer = new DataLinkLayer();
while(true); // to keep the main thread running
}

I, coruse, , . , DataLinkLayer cosntructor, ( ) isntances dataLinkLayer ( this). , . - , .

, , - , ( :):).

DataLinkLayer - , , . , , , , ..

, . , .

, , merci !

UPD:

iDataLinkLayer - ? Java , . 100% , ++... /commnets ?

0

Source: https://habr.com/ru/post/1780030/


All Articles