Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ ATL
I would appreciate it if someone can provide me with a code sample for using CAtlArray as a member variable inside a class, where the array should hold a custom data structure.
 
I have defined the data structure:
typedef struct
{
	int Status;
	CTime StatusDate;
} MyDataStructure;
 
Then, in my class, I define:
 
private:
	CAtlArray<MyDataStructure> MyArray;
 
When I call RemoveAll(), or try to add a new item, I get an Access Violation error.
I tried to use SetCount() prior to adding members, but it didn't work as well.
Posted 6-Oct-12 12:21pm
Comments
cariolihome at 6-Oct-12 19:18pm
   
Please, show Your code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

May be you have missed to malloc first before insert into MyArray.
MyDataStructure* pNewStruct;
pNewStruct =(MyDataStructure* )malloc(sizeof(MyDataStructure));
MyArray.Add(pNewStruct);
Hope this will work.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This work for me and i get value after thread exit. Compare your code with me , you can understand your problem. Another thing CreateThread can not call a member function, so you must need a static function for this.,
 
#include "stdafx.h"
#include <atlbase.h>
#include <atltime.h>
#include <atlcoll.h>
#include <atldebugapi.h>
 
class MyClass
{
public:
	typedef struct
	{
		CString* FullPath;
		CTime* StatusDate;
		MyClass* pClass;
	} _Item;
 
	MyClass();
	~MyClass();
	void StartThread();
	void* MyThread(void* arg);
	typedef _Item Item;
	Item MyItem;
 
	static void _MyThread(void* lpParam)
	{
		Item* obj = (Item* )lpParam;
		
		MyClass* a = obj->pClass;
		a->MyThread(lpParam);
	}
//private:
	CAtlArray<item*> MyArray;
};
 

MyClass::MyClass()
{
}
void MyClass::StartThread()
{
	HANDLE handle;
	int val=0;
	Item* MyNewItem = (Item* )malloc(sizeof(Item));
	//wcscpy(MyNewItem.FullPath.GetBuffer(),L"c:\\");
	MyNewItem->FullPath = new CString(L"c:\\");
	MyNewItem->pClass = this;
	handle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)&_MyThread, MyNewItem, 0, NULL);  
	if ( handle == NULL)  ExitProcess(0);
 
}
void* MyClass::MyThread(void* arg)
{
	Item* newObj = (Item* )arg; 
	MyArray.Add(newObj);
	return 0;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	MyClass *myClass = new MyClass();
	myClass->StartThread();
	Sleep(200);  
	CString str;
	str.Format(L"\n\n--- PATH=%s --- \n\n",*(myClass->MyArray.GetAt(0)->FullPath));
	OutputDebugString(str);
 
	return 1;
}
</atldebugapi.h></atlcoll.h></atltime.h></atlbase.h>
 
Best of luck Big Grin | :-D
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

How can I get notified, to the main even loop when this thread completes?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Something like
 
// testproject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlbase.h>
#include <atltime.h>
#include <atlcoll.h>
typedef struct
{
	CString FullPath;
	CTime StatusDate;
} Item;
 
class MyClass
{
public:
	MyClass();
	~MyClass();
	void StartThread();
	void* MyThread(void* arg);
	Item MyItem;
private:
	CAtlArray<item> *MyArray;
};
 
void MyClass::StartThread(void)
{
	HANDLE handle;
	int val=0;
	Item MyNewItem;
	wcscpy(MyNewItem.FullPath.GetBuffer(),L"c:\\");
	
	handle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) &MyClass::MyThread, &MyNewItem, 0, NULL);  
    if ( handle == NULL)  ExitProcess(0);
 
}
void* MyClass::MyThread(void* arg)
{
	Item* newObj = (Item* )arg; 
	MyArray->Add(*Item);
	
	return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
	MyClass *myClass = new MyClass;
	myClass->StartThread();
	return 1;
}
 

 

</item></atlcoll.h></atltime.h></atlbase.h>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

[ bappycse05] is correct but I also used a Thread which is a member function of a class and wanted it to access the class's member variables, but the moment the thread ends, any changes it makes in any member variables are erased.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It will be better if you post a little bit of code. However,
suppose classname is MyClass and your thread name MyThread. You call this thread from your class.
then try this..
 
 
_beginthread(MyThread,0,this);
 
void* MyThread(void* arg)
{
   MyClass* newObj = (MyClass* )arg; 
  // now you can use all variable or method of this class using this object;
  newObj->MyArray.add(YourStructure); 
}
 
if this will not work for you then please post some code.
 
Well, try this code:
 
// testproject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlbase.h>
#include <atltime.h>
#include <atlcoll.h>
typedef struct
{
	CString FullPath;
	CTime StatusDate;
} Item;
 
class MyClass
{
public:
	MyClass();
	~MyClass();
	void StartThread();
	void* MyThread(void* arg);
	Item MyItem;
private:
	CAtlArray<item> *MyArray;
};
 
void MyClass::StartThread(void)
{
	HANDLE handle;
	int val=0;
	Item MyNewItem;
	wcscpy(MyNewItem.FullPath.GetBuffer(),L"c:\\");
	
	handle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) &MyClass::MyThread, &MyNewItem, 0, NULL);  
    if ( handle == NULL)  ExitProcess(0);
 
}
void* MyClass::MyThread(void* arg)
{
	Item* newObj = (Item* )arg; 
	MyArray->Add(*Item);
	
	return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
	MyClass *myClass = new MyClass;
	myClass->StartThread();
	return 1;
}
  Permalink  
v2
Comments
   
It looks to me as if any change you make inside the thread doesn't affect the data structure after the thread has ended, so MyArray is not updated. Why don't you check and see.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Use this ---
dwRet = WaitForSingleObject(HANDLE,TIME);
 
see the example ..
http://www.installsetupconfig.com/win32programming/threadprocesssynchronizationapis11_13.html[^]
 
when thread will be completed, use postmessage method to inform UI Big Grin | :-D
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 400
1 Gihan Liyanage 238
2 Sergey Alexandrovich Kryukov 150
3 ChauhanAjay 120
4 syed shanu 110
0 Sergey Alexandrovich Kryukov 9,021
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100