Member Avatar for Member #842639

I'm using Dev-c++ to make a dialog-based app. I had it working, but now I am getting an error:
An Access Violation (Segmentation Fault) raised in your program.

I have narrowed it down to the winmain function:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
   if (iCmdShow > 4) {
      commandlineopener=szCmdLine;
   }
   gCmdShow=iCmdShow;
   strcat(about,version);
   strcat(about,"Stuff"); //About stuffs go here
   DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0,(DLGPROC)MainDlgProc);
   WSACleanup();
   return 0;
}

What am I doing wrong?

Dani AI

Generated

This is a textbook case of earlier memory corruption showing up later (crash at WSACleanup is a symptom, not the root). traced it to the strcat() usage and removing those calls stopped the crash — exactly what you'd expect if about was being overrun or if version wasn't a proper NUL-terminated string. was right to ask for the about definition.

Strcat is unsafe because it doesn't check available space. Fix by composing the final string with a bounded API or with C++ strings. Example (C-style, safe):

char about[128];
const char *prefix = "My app, version ";
snprintf(about, sizeof(about), "%s%s %s", prefix, version, "Stuff");

Or use std::string to avoid manual buffer arithmetic:

std::string about = std::string(prefix) + version + " Stuff";

Quick troubleshooting checklist that would have pinpointed the bug faster:

  • Print strlen(about) and strlen(version) before/after the concatenations to see if lengths exceed the buffer.
  • Replace strcat() with strncat()/snprintf() or prefer std::string.
  • Verify version is a valid, NUL-terminated pointer and that commandlineopener assignment matches its declared type (pointer vs array).
  • Run under a debugger and set breakpoints after each string operation; heap/stack corruption often only crashes later.
  • If using MSVC, enable CRT debug heap or address sanitizers to catch overruns.

Takeaway: even if the crash shows up on WSACleanup, the offending code was earlier. Replace unsafe strcat() uses with bounded formatting or C++ string operations and add a simple length check — that will prevent this class of intermittent crashes.

Recommended Answers

All 4 Replies

You strcat version and "Stuff" into about...What's 'about'? Can we see its definition?

Member Avatar for Member #842639

I have it defined as global, right after the includes:

char about[128]="C++ dialog-based plain text editor, version ";

But I had that before, and it was working, so I don't think that's the problem...

Member Avatar for Member #842639

I tried adding a MessageBox after each action in the function. The program actually crashes after WSACleanup. But that can't be right, can it? I've never heard of a problem with the return...

Member Avatar for Member #842639

OK. I figured it out. Apparently, the whole strcat() stuff was doing something that it didn't like, and I moved all the data to the declaration of about. After I deleted the strcat()'s, it no longer crashed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.