1

I'm trying to make a multithreaded program.
I can compile it successfully, but my program always crashes.

Here is my code snippet :

// in global declaration
typedef struct MyData {
    int nSumber;
    char *szFileName;
} MYDATA, *PMYDATA;

PMYDATA pData[MAX_THREAD];

// in my OpenDialog function
OPENFILENAME ofn;
char szFile[MAX_PATH];
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
if(GetOpenFileName(&ofn))
    // I think this line that makes my program crash
    pData[0]->szFileName = ofn.lpstrFile;

Am I doing it wrong?

Thanks in advance!

phant0m
  • 16,595
  • 5
  • 50
  • 82
user1888256
  • 167
  • 1
  • 2
  • 8

3 Answers3

2

The pData array contains pointers to MYDATA that are not pointing to valid memory because you haven't allocated any memory yet.

if(GetOpenFileName(&ofn)) {
  pData[0] = new MYDATA();
  pData[0]->szFileName = ofn.lpstrFile;
}

And now you need to call delete on each allocated pData element to release the memory.

So, instead of going through all that trouble, use an std::vector<MYDATA>.

std::vector<MYDATA> dataVec;

// ...

if(GetOpenFileName(&ofn)) {
  MYDATA data;
  data.szFileName = ofn.lpstrFile;
  data.nSumber = something; // you probably want to init this also
  dataVec.push_back(data);
}

I'm not sure about szFileName being a char * and simply pointing it to ofn.lpstrFile. You may need to allocate memory and store the file name in the struct. If that's the case, change szFileName to std::string instead of char *.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • I have try std::string instead of char * but the problem still goes on. Is there any other simple way (i'm beginner) for retrieving a file name and assign it to struct ? Thanks in advance – user1888256 Dec 12 '12 at 18:52
  • @user1888256 It's hard to say what problems you're having given the code you've posted. If you compile and run in debug mode, your debugger should automatically break when an exception is encountered and tell you what line the error happened at. – Praetorian Dec 12 '12 at 19:20
  • i use devcpp and this is what igot from debugger, "Thread 1 stopped in OpenDialog(HWND__*, int) at line 5808667 in with Segmentation fault". But i have fixed the crash problem by following this [link](http://stackoverflow.com/questions/8847370/using-malloc-to-allocate-memory-for-a-string-c-source). But know, i face new problem, i can't convert my struct to LPVOID to CreateThread() function. Any idea? Thanks in advance – user1888256 Dec 12 '12 at 19:44
0

Your question leaves out a lot of detail, but if char szFile[MAX_PATH] is local to a function it is on the stack. You can't save it in a global variable and expect it to be used elsewhere because the stack space will be re-used as soon as szFile goes out of scope (at the next } or the end of the function).

For a quick test try ofn.lpstrFile = new char[MAX_PATH] and see if your crash goes away.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
0

This only allocates an array of pointers to the data:

PMYDATA pData[MAX_THREAD];

You need to actually allocate a MYDATA at pData[0] for this to be valid.

gil_bz
  • 478
  • 1
  • 5
  • 16
  • Thanks for your quick reply gil. I have try your advise but it still keeps crasing. I just follow from this [link](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516%28v=vs.85%29.aspx). – user1888256 Dec 12 '12 at 18:32