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 9269891
In Process

The Archive Base Latest Questions

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

I’m doing some socket programming lately and starting to code in C++. As the

  • 0

I’m doing some socket programming lately and starting to code in C++. As the program I’m trying to write will have several components that have to communicate via sockets I decided to wrap up socket communication in a class.

Since there are several errors that can happen I decided to create a class for socket exceptions, I defined it like this:

class SocketException: public std::exception {
public:
  SocketException(const std::string &message);
  ~SocketException() throw();
  virtual const char * what() const throw();
private:
  std::string msg;
};

The implementation is as follows:

SocketException::SocketException(const std::string &message) : msg(message) {}
SocketException::~SocketException() throw() {}

const char * SocketException::what() const throw() {
  std::stringstream stream;
  stream << msg + " Error number: ";
  stream << WSAGetLastError();
  return stream.str().c_str();
}

The implementation of the what() method is incomplete as of now, because I’d like to show the text meaning of the error code via FormatMessage() but I haven’t written that yet.

I was trying this code in Visual Studio but instead of working as I expected the what() method returns garbage. After spending quite some time trying to figure out the problem and trying different things I ended up trying a different compiler.

Using MinGW (GCC) the code compiles and runs as expected, messages are shown as I thought they’d be (if anyone is interested I’m just trying to perform a connect() when not connected to the Internet).

I’m just learning C++, I’d like to know where the problem is or what would be the appropriate way of doing it.

EDIT: Thanks for the comment and answer, at first I thought that’d be the case so I used new to allocate the stream (even knowing it’d be a leak, just to try as I understand new uses the heap) and the result was the same, this is what I had:

const char * SocketException::what() const throw() {
  std::stringstream *stream = new std::stringstream();
  *stream << msg + " Error: ";
  *stream << WSAGetLastError();
  return (*stream).str().c_str();
}
  • 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:14:10+00:00Added an answer on June 18, 2026 at 3:14 pm

    You are returning a pointer to a variable that is local to your what function, thus leaving the caller with a dangling pointer:

    const char * SocketException::what() const throw() {
      std::stringstream stream;  // this stringstream will die on exiting this scope
      stream << msg + " Error number: ";
      stream << WSAGetLastError();
      return stream.str().c_str();
    }
    

    That is undefined behaviour. It means anything could happen.

    An example of a method safely returning const char* would be this (obviously it doesn’t have the intended functionality of the original, it is just for illustration purposes):

    const char * SocketException::what() const throw()
    {
      return msg.c_str(); // msg lives as long as this SocketException instance 
    }
    

    This has defined behaviour provided the pointer is not dereferenced after the SocketException object dies.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.