Skip to content

Commit 4acb82c

Browse files
author
bob.burrough
committed
Allocate the filename within the choose file context. I don't believe this was necessary, but it is a bit cleaner of an implementation. I did this while making changes trying to track down Unity crashes which turned out to be unrelated to the Natives library.
1 parent 1457bff commit 4acb82c

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

Natives.sdf

0 Bytes
Binary file not shown.

Natives/Natives.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
<GenerateDebugInformation>true</GenerateDebugInformation>
8686
<AdditionalLibraryDirectories>$(SolutionDir)ExternalProjects\pthreads-w32\Pre-built.2\lib\x86</AdditionalLibraryDirectories>
8787
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies);pthreadVC2.lib</AdditionalDependencies>
88+
<EntryPointSymbol>
89+
</EntryPointSymbol>
8890
</Link>
8991
</ItemDefinitionGroup>
9092
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

Natives/natives.cpp

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11

22
#define BLIT_IT_NATIVES_DLL_COMPILE
33

4-
#include "natives.h"
5-
64
#include "threadutils.h"
75

6+
7+
#include "natives.h"
8+
89
#include <list>
910
#include <string>
11+
#include <vector>
1012

1113
#include <Windows.h>
14+
#include <roapi.h>
1215

1316
using namespace std;
1417

18+
19+
1520
struct ChooseFileContext
1621
{
1722
ChooseFileSuccessCallback _chooseFileSuccessCallback;
1823
ChooseFileCancelledCallback _chooseFileCancelledCallback;
1924
pthread_t file_picker_thread;
2025
list<wstring> filetype_names;
2126
list<wstring> filetype_extensions;
27+
wchar_t* chosenFilename;
2228
};
2329

2430

2531
void* FilePickerThread(void* arg)
26-
{
32+
{
2733
ChooseFileContext* cfc = (ChooseFileContext*)arg;
2834

29-
const int buf_len = 260;
30-
//TCHAR cwd[buf_len] = { 0 };
31-
32-
//GetCurrentDirectory(buf_len, cwd);
35+
const int buf_len = 261;
36+
cfc->chosenFilename = new wchar_t[buf_len];
37+
memset(cfc->chosenFilename, 0, buf_len);
3338

3439
OPENFILENAME ofn; // common dialog box structure
35-
TCHAR szFile[buf_len]; // buffer for file name
36-
//HWND hwnd; // owner window
37-
//HANDLE hf; // file handle
38-
39-
4040

4141
//string test("G-code\0*.gcode\0All\0*.*\0");
4242
wstring filetype_filters;
@@ -54,24 +54,20 @@ void* FilePickerThread(void* arg)
5454
filetype_filters.push_back('\0');
5555
filetype_filters += L"*.*";
5656
filetype_filters.push_back('\0');
57-
57+
filetype_filters.push_back('\0');
5858

5959
//TCHAR filter[64] = TEXT(((const char*)test.c_str()));
6060

61-
TCHAR* filter = (TCHAR*)filetype_filters.c_str();
62-
63-
64-
61+
TCHAR* filter = (TCHAR*)filetype_filters.data();
6562

6663
// Initialize OPENFILENAME
6764
ZeroMemory(&ofn, sizeof(ofn));
6865
ofn.lStructSize = sizeof(ofn);
6966
//ofn.hwndOwner = hwnd;
70-
ofn.lpstrFile = szFile;
67+
ofn.lpstrFile = cfc->chosenFilename;
7168
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
7269
// use the contents of szFile to initialize itself.
73-
ofn.lpstrFile[0] = '\0';
74-
ofn.nMaxFile = buf_len;
70+
ofn.nMaxFile = buf_len - 1; // use -1 because we're paranoid that Windows will stupidly not include a null terminator
7571
ofn.lpstrFilter = filter;
7672
ofn.nFilterIndex = 1;
7773
ofn.lpstrFileTitle = NULL;
@@ -85,7 +81,9 @@ void* FilePickerThread(void* arg)
8581
{
8682
pthread_testcancel();
8783
if (cfc->_chooseFileSuccessCallback)
88-
cfc->_chooseFileSuccessCallback(ofn.lpstrFile);
84+
{
85+
cfc->_chooseFileSuccessCallback(cfc->chosenFilename);
86+
}
8987
}
9088
else
9189
{
@@ -103,6 +101,7 @@ void* __stdcall CreateChooseFileContext()
103101
ChooseFileContext* cfc = new ChooseFileContext();
104102
cfc->_chooseFileCancelledCallback = NULL;
105103
cfc->_chooseFileSuccessCallback = NULL;
104+
cfc->chosenFilename = NULL;
106105
return cfc;
107106
}
108107

@@ -128,6 +127,9 @@ void __stdcall DestroyChooseFileContext(void* context)
128127
{
129128
ChooseFileContext* cfc = (ChooseFileContext*)context;
130129
pthread_cancel(cfc->file_picker_thread);
130+
pthread_join(cfc->file_picker_thread, NULL);
131+
if (cfc->chosenFilename != NULL)
132+
delete[] cfc->chosenFilename;
131133
delete cfc;
132134
}
133135

0 commit comments

Comments
 (0)