Memory errors when calling variable
I'm getting different errors that are always related to the memory (this current set throws memory violation exception) when I pass a pointer to a function.
I tried various implementations to solve the problem but nothing worked.
Here is the code:
In class:
class OpenFileDialog
public:
OpenFileDialog();
~OpenFileDialog();
static bool BrowseFiles(wstring* fileName) OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn))
return true;
else
return false;
;
Method calling:
try
wstring fileName;
if (OpenFileDialog::BrowseFiles(&fileName))
A(fileName); // erros here
else
A(_T("Failed"));
catch (bad_alloc)
A(L"Bad allocation");
c++ winapi
add a comment |
I'm getting different errors that are always related to the memory (this current set throws memory violation exception) when I pass a pointer to a function.
I tried various implementations to solve the problem but nothing worked.
Here is the code:
In class:
class OpenFileDialog
public:
OpenFileDialog();
~OpenFileDialog();
static bool BrowseFiles(wstring* fileName) OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn))
return true;
else
return false;
;
Method calling:
try
wstring fileName;
if (OpenFileDialog::BrowseFiles(&fileName))
A(fileName); // erros here
else
A(_T("Failed"));
catch (bad_alloc)
A(L"Bad allocation");
c++ winapi
1
And what isA?
– Yksisarvinen
Nov 16 '18 at 10:00
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09
add a comment |
I'm getting different errors that are always related to the memory (this current set throws memory violation exception) when I pass a pointer to a function.
I tried various implementations to solve the problem but nothing worked.
Here is the code:
In class:
class OpenFileDialog
public:
OpenFileDialog();
~OpenFileDialog();
static bool BrowseFiles(wstring* fileName) OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn))
return true;
else
return false;
;
Method calling:
try
wstring fileName;
if (OpenFileDialog::BrowseFiles(&fileName))
A(fileName); // erros here
else
A(_T("Failed"));
catch (bad_alloc)
A(L"Bad allocation");
c++ winapi
I'm getting different errors that are always related to the memory (this current set throws memory violation exception) when I pass a pointer to a function.
I tried various implementations to solve the problem but nothing worked.
Here is the code:
In class:
class OpenFileDialog
public:
OpenFileDialog();
~OpenFileDialog();
static bool BrowseFiles(wstring* fileName) OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn))
return true;
else
return false;
;
Method calling:
try
wstring fileName;
if (OpenFileDialog::BrowseFiles(&fileName))
A(fileName); // erros here
else
A(_T("Failed"));
catch (bad_alloc)
A(L"Bad allocation");
c++ winapi
c++ winapi
asked Nov 16 '18 at 9:58
Vinícius GabrielVinícius Gabriel
151110
151110
1
And what isA?
– Yksisarvinen
Nov 16 '18 at 10:00
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09
add a comment |
1
And what isA?
– Yksisarvinen
Nov 16 '18 at 10:00
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09
1
1
And what is
A?– Yksisarvinen
Nov 16 '18 at 10:00
And what is
A?– Yksisarvinen
Nov 16 '18 at 10:00
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09
add a comment |
1 Answer
1
active
oldest
votes
The cast LPWSTR((*fileName).c_str()) should be a hint that you do something wrong.
The string fileName is empty, it doesn't have any memory allocated for it. Calling c_str() will return a pointer to a constant string of zero characters.
So there are two errors: You try to write data to memory that doesn't exist; And even if it did it would be constant and not modifiable anyway. Both leading to undefined behavior.
You need to create a temporary array of characters, large enough to hold the longest path possible, and then initialize fileName to that string after GetOpenFileNameW succeeds.
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-constdata(). C++11 allows forfileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.
– zett42
Nov 16 '18 at 12:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335384%2fmemory-errors-when-calling-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The cast LPWSTR((*fileName).c_str()) should be a hint that you do something wrong.
The string fileName is empty, it doesn't have any memory allocated for it. Calling c_str() will return a pointer to a constant string of zero characters.
So there are two errors: You try to write data to memory that doesn't exist; And even if it did it would be constant and not modifiable anyway. Both leading to undefined behavior.
You need to create a temporary array of characters, large enough to hold the longest path possible, and then initialize fileName to that string after GetOpenFileNameW succeeds.
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-constdata(). C++11 allows forfileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.
– zett42
Nov 16 '18 at 12:16
add a comment |
The cast LPWSTR((*fileName).c_str()) should be a hint that you do something wrong.
The string fileName is empty, it doesn't have any memory allocated for it. Calling c_str() will return a pointer to a constant string of zero characters.
So there are two errors: You try to write data to memory that doesn't exist; And even if it did it would be constant and not modifiable anyway. Both leading to undefined behavior.
You need to create a temporary array of characters, large enough to hold the longest path possible, and then initialize fileName to that string after GetOpenFileNameW succeeds.
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-constdata(). C++11 allows forfileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.
– zett42
Nov 16 '18 at 12:16
add a comment |
The cast LPWSTR((*fileName).c_str()) should be a hint that you do something wrong.
The string fileName is empty, it doesn't have any memory allocated for it. Calling c_str() will return a pointer to a constant string of zero characters.
So there are two errors: You try to write data to memory that doesn't exist; And even if it did it would be constant and not modifiable anyway. Both leading to undefined behavior.
You need to create a temporary array of characters, large enough to hold the longest path possible, and then initialize fileName to that string after GetOpenFileNameW succeeds.
The cast LPWSTR((*fileName).c_str()) should be a hint that you do something wrong.
The string fileName is empty, it doesn't have any memory allocated for it. Calling c_str() will return a pointer to a constant string of zero characters.
So there are two errors: You try to write data to memory that doesn't exist; And even if it did it would be constant and not modifiable anyway. Both leading to undefined behavior.
You need to create a temporary array of characters, large enough to hold the longest path possible, and then initialize fileName to that string after GetOpenFileNameW succeeds.
answered Nov 16 '18 at 10:04
Some programmer dudeSome programmer dude
304k25265426
304k25265426
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-constdata(). C++11 allows forfileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.
– zett42
Nov 16 '18 at 12:16
add a comment |
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-constdata(). C++11 allows forfileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.
– zett42
Nov 16 '18 at 12:16
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
SO users tend to post the last version of the code they tried. It was the only "memory error" cause he could think of. It ain't it.
– Hans Passant
Nov 16 '18 at 10:12
A temporary C-style character array is not needed.
fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-const data(). C++11 allows for fileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.– zett42
Nov 16 '18 at 12:16
A temporary C-style character array is not needed.
fileName->resize(MAX_PATH); ofn.lpstrFile = fileName->data();. This requires C++17 for non-const data(). C++11 allows for fileName->resize(MAX_PATH); ofn.lpstrFile = &fileName[0];. Valid, but less clean.– zett42
Nov 16 '18 at 12:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335384%2fmemory-errors-when-calling-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
And what is
A?– Yksisarvinen
Nov 16 '18 at 10:00
Just a function that prints the passing variable in a messageBox. However, thats not the issue since. I used the function multiple times.
– Vinícius Gabriel
Nov 16 '18 at 10:02
The debugger stack trace is very important. GetOpenFileName() and friends are risky, they load shell extensions in your process. Programmer machines tend to have a lot of them, not always of the best quality. If one of them misbehaves then the debugger will see it.
– Hans Passant
Nov 16 '18 at 10:09