Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:43:59+00:00 2026-06-18T15:43:59+00:00

Error message is: The instruction at 0x7c810eac referenced memory at 0x00000000. The memory could

  • 0

Error message is: The instruction at “0x7c810eac” referenced memory at “0x00000000”. The memory could not be “written”.

If I remove the destructor everything is fine. But I do not understand what is happening here. Everywhere I read that I should close handles, but here code won’t let me. (Yes I know that I can do it manually… but that is a unnecessary line in client code that I think should be handled by object.)

#include <windows.h>
#include <iostream>
#include <string>

struct fileHandle {
  HANDLE hFile;
  fileHandle(std::string path) {
    hFile = CreateFile(path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
      printf("error: INVALID_HANDLE_VALUE");
    }
  }
  ~fileHandle() {
    CloseHandle(hFile);
  }
  void save(std::string data) {
    if (!WriteFile(hFile, data.c_str(), data.size(), NULL, NULL)) {
      printf("WriteFile failed: ", GetLastError());
    }
  }
};

int main() {
  fileHandle my_handle("test_file.txt");
  my_handle.save("some text");
}

Update: this happens when file doesn’t exist. When file do exist program print errors, but this is intended. I’m here asking co cover only this case when file is created (I know how to rewrite handle creating to cover existing file.)

compiler: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev7.7z

Update 2: I didn’t mention that this code works and writes to file. Memory error is triggered at the very end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:44:01+00:00Added an answer on June 18, 2026 at 3:44 pm

    Please compile all your code with -Wall. Saves a whole lot amount of time.

    In your code printf which has an invalid format string. Correct solution for printf would be (notice the %lu):

    void save(std::string data) {
      if (!WriteFile(hFile, data.c_str(), data.size(), NULL, NULL)) {
        printf("WriteFile failed: %lu", GetLastError());
      }
    }
    

    If you would have compiled with -Wall, your code would have given the warning:

    filehandle.cpp: In member function 'void fileHandle::save(std::string)':
    filehandle.cpp:18:50: warning: too many arguments for format [-Wformat-extra-args]
    

    Also you should print errors to stderr as it is unbuffered. printf uses buffers and that’s why you did not get any output. Also a good idea to add a \n after the errors.

    Also read the other answers to improve your code. Use the rule of three.


    After reading the comment I realized that this is not the segfault reason indeed. (Also look at Ron Burk’s solution to see what went wrong.)

    According to the Windows API documentation, lpNumberOfBytesWritten parameter can be NULL only when the lpOverlapped parameter is not NULL.

    So you have to give a pointer to a DWORD where WriteFile can store how many bytes it actually read. The final save would be:

    void save(std::string data) {
      DWORD writtenBytes;
      if (!WriteFile(hFile, data.c_str(), data.size(), &writtenBytes, NULL)) {
        printf("WriteFile failed: %lu", GetLastError());
      }
    }
    

    If the file exists, the error does not pop up, because passing INVALID_HANDLE_VALUE to WriteFile seems to make WriteFile return earlier than it uses your pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Error message: Request for member nameField in something not a struct or union
As the error message stated below, I do not use user_profiles_path as plural because
I get this error message pageContext cannot be resolved but I have no variable
I got the following error message while compiling, but I'm sure I have the
Error message doesn't display on mysql connect failure if( ! $this->remote_connection_id = @mysql_connect($vars['hostname'], $vars['username'],
When error message is a long string, how to make the message be displayed
The error message I gen when I try to access the web page server
Got this error message while trying to load view: The model item passed into
I want to display error message or alert message after checking the condition. here
I get this error message in LogCat: 06-25 15:17:34.495: E/AndroidRuntime(5891): java.lang.RuntimeException: Unable to start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.