teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

8

解決策を追加した

2016/07/04 14:00

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -217,4 +217,7 @@
217
217
  マシンA (Windows 7 Ultimate x64)
218
218
  マシンB (Windows 10 Pro x64)
219
219
  マシンC (Windows 10 Home x86)
220
- マシンD (Windows 7 Professional x64)
220
+ マシンD (Windows 7 Professional x64)
221
+
222
+ ###解決策 (by catsforepawさん)
223
+ **CoInitialize**と**CoUninitialize**を呼ぶようにする。

7

追記

2016/07/04 14:00

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -204,6 +204,7 @@
204
204
  マシンA … 最初に試したマシン => コンパイル・実行 => 実行時に強制終了
205
205
  マシンB … 次に試したマシン => コンパイル・実行 => 異常なし
206
206
  マシンC … その次に試したマシン => マシンAとマシンBでビルドしたexeを実行 => どちらも異常なし
207
+ マシンD … その後試したマシン => マシンAとマシンBでビルドしたexeを実行 => どちらも実行時に強制終了
207
208
 
208
209
  その後マシンAを再起動させてもう一度コンパイル・実行したところ
209
210
  実行時に強制終了してしまいました。

6

2016/07/04 11:57

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -215,4 +215,5 @@
215
215
 
216
216
  マシンA (Windows 7 Ultimate x64)
217
217
  マシンB (Windows 10 Pro x64)
218
- マシンC (Windows 10 Home x86)
218
+ マシンC (Windows 10 Home x86)
219
+ マシンD (Windows 7 Professional x64)

5

修正

2016/07/04 07:13

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -201,9 +201,9 @@
201
201
  **7/4追記**
202
202
  別のマシンでコンパイル・実行したところ
203
203
  特に強制終了することなく動作しました。
204
- マシンA (Windows 7 Ultimate x64) … 最初に試したマシン => コンパイル・実行 => 実行時に強制終了
204
+ マシンA … 最初に試したマシン => コンパイル・実行 => 実行時に強制終了
205
- マシンB (Windows 10 Pro x64) … 次に試したマシン => コンパイル・実行 => 異常なし
205
+ マシンB … 次に試したマシン => コンパイル・実行 => 異常なし
206
- マシンC (Windows 10 Home x86) … その次に試したマシン => マシンBでビルドしたexeを実行 => 異常なし
206
+ マシンC … その次に試したマシン => マシンAとマシンBでビルドしたexeを実行 => どちらも異常なし
207
207
 
208
208
  その後マシンAを再起動させてもう一度コンパイル・実行したところ
209
209
  実行時に強制終了してしまいました。
@@ -211,4 +211,8 @@
211
211
  ###補足情報(言語/FW/ツール等のバージョンなど)
212
212
  Visual Studio Commnunity 2015
213
213
  Direct2D (d2d1.h) (d2d1.lib) (dxguid.lib)
214
- WIC (wincodec.h) (windowscodecs.lib)
214
+ WIC (wincodec.h) (windowscodecs.lib)
215
+
216
+ マシンA (Windows 7 Ultimate x64)
217
+ マシンB (Windows 10 Pro x64)
218
+ マシンC (Windows 10 Home x86)

4

試したことを追加した。

2016/07/04 06:20

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -198,6 +198,16 @@
198
198
  bitmapのReleaseを最初に行うように変更してみましたが、
199
199
  アクセス違反が発生していました。
200
200
 
201
+ **7/4追記**
202
+ 別のマシンでコンパイル・実行したところ
203
+ 特に強制終了することなく動作しました。
204
+ マシンA (Windows 7 Ultimate x64) … 最初に試したマシン => コンパイル・実行 => 実行時に強制終了
205
+ マシンB (Windows 10 Pro x64) … 次に試したマシン => コンパイル・実行 => 異常なし
206
+ マシンC (Windows 10 Home x86) … その次に試したマシン => マシンBでビルドしたexeを実行 => 異常なし
207
+
208
+ その後マシンAを再起動させてもう一度コンパイル・実行したところ
209
+ 実行時に強制終了してしまいました。
210
+
201
211
  ###補足情報(言語/FW/ツール等のバージョンなど)
202
212
  Visual Studio Commnunity 2015
203
213
  Direct2D (d2d1.h) (d2d1.lib) (dxguid.lib)

3

コメントをつけた。

2016/07/04 06:18

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
 
16
16
  ###該当のソースコード
17
17
  7/4更新
18
- (ファイルを分けてあるコードも今はまとめて1つファイルに書いてます。)
18
+ (実際はファイルを分けてコード書いてあります。)
19
19
  ```C++
20
20
  #include <d2d1.h>
21
21
  #include <wincodec.h>

2

コードを更新した。

2016/07/03 19:26

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -47,33 +47,23 @@
47
47
  ID2D1SolidColorBrush* brush = NULL;
48
48
 
49
49
  // Direct2Dの初期化処理を行う
50
- bool initializeDirect2D()
50
+ void initializeDirect2D()
51
51
  {
52
- HRESULT result;
53
-
54
52
  // Direct2Dファクトリを生成する
55
- result = D2D1CreateFactory(
53
+ D2D1CreateFactory(
56
54
  D2D1_FACTORY_TYPE_SINGLE_THREADED,
57
55
  &direct2DFactory);
58
- if (FAILED(result))
59
- return false;
60
56
 
61
57
  // レンダーターゲットを生成する
62
- result = direct2DFactory->CreateHwndRenderTarget(
58
+ direct2DFactory->CreateHwndRenderTarget(
63
59
  D2D1::RenderTargetProperties(),
64
60
  D2D1::HwndRenderTargetProperties(
65
61
  windowHandle,
66
62
  D2D1::SizeU(WINDOW_WIDTH, WINDOW_HEIGHT)),
67
63
  &renderTarget);
68
- if (FAILED(result))
69
- return false;
70
64
 
71
65
  // ブラシを生成する
72
- result = renderTarget->CreateSolidColorBrush(D2D1::ColorF(0, 0, 0), &brush);
66
+ renderTarget->CreateSolidColorBrush(D2D1::ColorF(0, 0, 0), &brush);
73
- if (FAILED(result))
74
- return false;
75
-
76
- return true;
77
67
  }
78
68
 
79
69
  // Direct2Dの終了処理を行う
@@ -107,56 +97,42 @@
107
97
 
108
98
  Image::Image(const wchar_t* fileName)
109
99
  {
110
- HRESULT result;
111
-
112
100
  // WICイメージングファクトリを生成する
113
- result = CoCreateInstance(
101
+ CoCreateInstance(
114
102
  CLSID_WICImagingFactory,
115
103
  NULL,
116
104
  CLSCTX_INPROC_SERVER,
117
105
  IID_IWICImagingFactory,
118
106
  (LPVOID*)&wicImagingFactory);
119
- if (FAILED(result))
120
- return;
121
107
 
122
108
  // Bitmapデコーダを生成する
123
- result = wicImagingFactory->CreateDecoderFromFilename(
109
+ wicImagingFactory->CreateDecoderFromFilename(
124
110
  fileName,
125
111
  NULL,
126
112
  GENERIC_READ,
127
113
  WICDecodeMetadataCacheOnLoad,
128
114
  &wicBitmapDecoder);
129
- if (FAILED(result))
130
- return;
131
115
 
132
116
  // 画像からフレームを読み込む
133
- result = wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
117
+ wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
134
- if (FAILED(result))
135
- return;
136
118
 
137
119
  // フォーマットコンバータを生成する
138
- result = wicImagingFactory->CreateFormatConverter(&wicFormatConverter);
120
+ wicImagingFactory->CreateFormatConverter(&wicFormatConverter);
139
- if (FAILED(result))
140
- return;
141
121
 
142
122
  // コンバータの初期化をする
143
- result = wicFormatConverter->Initialize(
123
+ wicFormatConverter->Initialize(
144
124
  wicBitmapFrame,
145
125
  GUID_WICPixelFormat32bppPBGRA,
146
126
  WICBitmapDitherTypeNone,
147
127
  NULL,
148
128
  0,
149
129
  WICBitmapPaletteTypeCustom);
150
- if (FAILED(result))
151
- return;
152
130
 
153
131
  // Bitmapを生成する
154
- result = renderTarget->CreateBitmapFromWicBitmap(
132
+ renderTarget->CreateBitmapFromWicBitmap(
155
133
  wicFormatConverter,
156
134
  NULL,
157
135
  &bitmap);
158
- if (FAILED(result))
159
- return;
160
136
  }
161
137
 
162
138
  Image::~Image()
@@ -184,16 +160,13 @@
184
160
  int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
185
161
  {
186
162
  // ウィンドウを生成する
187
- if (!initializeWin32API())
163
+ initializeWin32API();
188
- return -1;
189
164
 
190
165
  // Direct2Dの初期化処理を行う
191
- if (!initializeDirect2D())
166
+ initializeDirect2D();
192
- return -1;
193
167
 
194
168
  // DirectInputの初期化処理を行う
195
- if (!initializeDirectInput())
169
+ initializeDirectInput();
196
- return -1;
197
170
 
198
171
  Image* image = new Image(L"hoge.png");
199
172
 

1

コードを追加した。

2016/07/03 19:25

投稿

aridai1221
aridai1221

スコア45

title CHANGED
File without changes
body CHANGED
@@ -14,13 +14,88 @@
14
14
  ```
15
15
 
16
16
  ###該当のソースコード
17
+ 7/4更新
18
+ (ファイルを分けてあるコードも今はまとめて1つファイルに書いています。)
17
19
  ```C++
20
+ #include <d2d1.h>
21
+ #include <wincodec.h>
22
+ #include <map>
23
+
24
+ #pragma comment(lib, "d2d1.lib")
25
+ #pragma comment(lib, "dxguid.lib")
26
+ #pragma comment(lib, "windowscodecs.lib")
27
+
28
+ // ウィンドウの幅
29
+ extern int WINDOW_WIDTH;
30
+
31
+ // ウィンドウの高さ
32
+ extern int WINDOW_HEIGHT;
33
+
34
+ // アプリケーションのインスタンスハンドル
35
+ extern HINSTANCE applicationInstance;
36
+
37
+ // ウィンドウハンドル
38
+ extern HWND windowHandle;
39
+
40
+ // Direct2Dファクトリ
41
+ ID2D1Factory*direct2DFactory = NULL;
42
+
43
+ // レンダーターゲット
44
+ ID2D1HwndRenderTarget* renderTarget = NULL;
45
+
46
+ // ブラシ
47
+ ID2D1SolidColorBrush* brush = NULL;
48
+
49
+ // Direct2Dの初期化処理を行う
50
+ bool initializeDirect2D()
51
+ {
52
+ HRESULT result;
53
+
54
+ // Direct2Dファクトリを生成する
55
+ result = D2D1CreateFactory(
56
+ D2D1_FACTORY_TYPE_SINGLE_THREADED,
57
+ &direct2DFactory);
58
+ if (FAILED(result))
59
+ return false;
60
+
61
+ // レンダーターゲットを生成する
62
+ result = direct2DFactory->CreateHwndRenderTarget(
63
+ D2D1::RenderTargetProperties(),
64
+ D2D1::HwndRenderTargetProperties(
65
+ windowHandle,
66
+ D2D1::SizeU(WINDOW_WIDTH, WINDOW_HEIGHT)),
67
+ &renderTarget);
68
+ if (FAILED(result))
69
+ return false;
70
+
71
+ // ブラシを生成する
72
+ result = renderTarget->CreateSolidColorBrush(D2D1::ColorF(0, 0, 0), &brush);
73
+ if (FAILED(result))
74
+ return false;
75
+
76
+ return true;
77
+ }
78
+
79
+ // Direct2Dの終了処理を行う
80
+ void finalizeDirect2D()
81
+ {
82
+ OutputDebugString("brushをReleaseします!\n");
83
+ if (brush != NULL)
84
+ brush->Release();
85
+ OutputDebugString("renderTargetをReleaseします!\n");
86
+ if (renderTarget != NULL)
87
+ renderTarget->Release();
88
+ OutputDebugString("direct2DFactoryをReleaseします!\n");
89
+ if (direct2DFactory != NULL)
90
+ direct2DFactory->Release();
91
+ }
92
+
18
93
  class Image
19
94
  {
20
95
  public:
21
96
  Image(const wchar_t*);
22
97
  ~Image();
23
- void draw(float x, float y){};
98
+ void draw(float x, float y);
24
99
 
25
100
  private:
26
101
  IWICImagingFactory* wicImagingFactory = NULL;
@@ -32,59 +107,119 @@
32
107
 
33
108
  Image::Image(const wchar_t* fileName)
34
109
  {
110
+ HRESULT result;
111
+
35
112
  // WICイメージングファクトリを生成する
36
- CoCreateInstance(
113
+ result = CoCreateInstance(
37
114
  CLSID_WICImagingFactory,
38
115
  NULL,
39
116
  CLSCTX_INPROC_SERVER,
40
117
  IID_IWICImagingFactory,
41
118
  (LPVOID*)&wicImagingFactory);
119
+ if (FAILED(result))
120
+ return;
42
121
 
43
122
  // Bitmapデコーダを生成する
44
- wicImagingFactory->CreateDecoderFromFilename(
123
+ result = wicImagingFactory->CreateDecoderFromFilename(
45
124
  fileName,
46
125
  NULL,
47
126
  GENERIC_READ,
48
127
  WICDecodeMetadataCacheOnLoad,
49
128
  &wicBitmapDecoder);
129
+ if (FAILED(result))
130
+ return;
50
131
 
51
132
  // 画像からフレームを読み込む
52
- wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
133
+ result = wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
134
+ if (FAILED(result))
135
+ return;
53
136
 
54
137
  // フォーマットコンバータを生成する
55
- wicImagingFactory->CreateFormatConverter(&wicFormatConverter);
138
+ result = wicImagingFactory->CreateFormatConverter(&wicFormatConverter);
139
+ if (FAILED(result))
140
+ return;
56
141
 
57
142
  // コンバータの初期化をする
58
- wicFormatConverter->Initialize(
143
+ result = wicFormatConverter->Initialize(
59
144
  wicBitmapFrame,
60
145
  GUID_WICPixelFormat32bppPBGRA,
61
146
  WICBitmapDitherTypeNone,
62
147
  NULL,
63
148
  0,
64
149
  WICBitmapPaletteTypeCustom);
150
+ if (FAILED(result))
151
+ return;
65
152
 
66
153
  // Bitmapを生成する
67
- renderTarget->CreateBitmapFromWicBitmap(
154
+ result = renderTarget->CreateBitmapFromWicBitmap(
68
155
  wicFormatConverter,
69
156
  NULL,
70
157
  &bitmap);
158
+ if (FAILED(result))
159
+ return;
71
160
  }
72
161
 
73
162
  Image::~Image()
74
163
  {
164
+ OutputDebugString("bitmapをReleaseします!\n");
165
+ if (bitmap != NULL)
166
+ bitmap->Release();
167
+ OutputDebugString("wicFormatConverterをReleaseします!\n");
168
+ if (wicFormatConverter != NULL)
169
+ wicFormatConverter->Release();
170
+ OutputDebugString("wicBitmapFrameをReleaseします!\n");
171
+ if (wicBitmapFrame != NULL)
172
+ wicBitmapFrame->Release();
173
+ OutputDebugString("wicBitmapDecoderをReleaseします!\n");
174
+ if (wicBitmapDecoder != NULL)
175
+ wicBitmapDecoder->Release();
176
+ OutputDebugString("wicImagingFactoryをReleaseします!\n");
75
177
  if (wicImagingFactory != NULL)
76
178
  wicImagingFactory->Release();
77
- if (wicBitmapDecoder != NULL)
78
- wicBitmapDecoder->Release();
79
- if (wicBitmapFrame != NULL)
80
- wicBitmapFrame->Release();
81
- if (wicFormatConverter != NULL)
82
- wicFormatConverter->Release();
83
- if (bitmap != NULL)
84
- bitmap->Release();
85
179
  }
180
+
181
+ void Image::draw(float x, float y){}
86
182
  ```
183
+ ```C++
184
+ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
185
+ {
186
+ // ウィンドウを生成する
187
+ if (!initializeWin32API())
188
+ return -1;
87
189
 
190
+ // Direct2Dの初期化処理を行う
191
+ if (!initializeDirect2D())
192
+ return -1;
193
+
194
+ // DirectInputの初期化処理を行う
195
+ if (!initializeDirectInput())
196
+ return -1;
197
+
198
+ Image* image = new Image(L"hoge.png");
199
+
200
+ // 終了メッセージが送られるまでループする
201
+ while (checkMessage())
202
+ {
203
+ beginRendering();
204
+
205
+ clearScreen(1, 1, 1);
206
+ image->draw(0, 0);
207
+
208
+ endRendering();
209
+ }
210
+
211
+ delete image;
212
+
213
+ // Direct2Dの終了処理を行う
214
+ finalizeDirect2D();
215
+
216
+ // DirectInputの終了処理を行う
217
+ finalizeDirectInput();
218
+
219
+ return 0;
220
+ }
221
+ ```
222
+
88
223
  ###試したこと
89
224
  Release行う順番が良くないかと思い、
90
225
  bitmapのReleaseを最初に行うように変更してみましたが、