Thread: Application crash when using substr on AIX

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Application crash when using substr on AIX

    Hi,

    I am trying to use substr on a large string, 20-30MB.
    The same code compiles and run on other platforms (linux, HP, Windows) with no problem, but when running on AIX 6.1, it crashes.

    Seems like a memory problem, maybe process memory limit was reached, how can I find out?
    When I sorround the substr call with a try/catch I catch the exception, however I use catch (...) as I'm not sure what exception should be caught, and I would like to print it so maybe it will give me a clue.

    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Maybe we could help you if we saw the actual code.
    Anything that STL throws inherits std::exception.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by kmdv View Post
    Maybe we could help you if we saw the actual code.
    Anything that STL throws inherits std::exception.
    Here is the code:

    Code:
    string double_newline("\r\n\r\n");
    string::size_type reply_starts_at = reply.find(double_newline);
    if (reply_starts_at == string::npos) 
    {
      throw http_exception("http_connection::parse_reply(): unable to determine reply start");
    }
    
    return reply.substr(reply_starts_at + double_newline.length());
    Thanks.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    The code is correct. Keep in mind that something completely different and irrevelant may cause this crash. If the strings are really long, check string::max_size() but I doubt it will return something below your lengths. If you cannot catch an exception with (...) this is not a C++ exception. This part is correct, even if something went wrong it would throw a software, catchable exception (std::bad_alloc or std::out_of_range).

    EDIT:
    If you do catch the exception, just use:
    Code:
    try
    {
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
    Last edited by kmdv; 05-26-2011 at 03:42 AM.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    Okay, I was able to catch the exception, it is "bad allocation".
    Also, reply.max_size() returned "-3".

    Any idea?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You implicitly converted max_size() to a signed integer type, hence -3.
    You are running out of memory so you need a different approach for storing part of this string (for example, using index and length variables).

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    Indeed, when I used %u I got 4,294,967,293 for max_size() which is a lot more than I need.
    If the process is running out of memory, maybe I can adjust that?
    This small piece of code is part of a very large implementation, it is not possible rigth now to make changes to the way we process the response, i have to figure a way to make it work =\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet another problem with substr
    By manasij7479 in forum C++ Programming
    Replies: 12
    Last Post: 05-26-2011, 12:43 PM
  2. substr problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 05:48 AM
  3. Substr
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-04-2006, 01:44 AM
  4. using char with .substr()
    By sunburnbyRA in forum Windows Programming
    Replies: 2
    Last Post: 02-24-2004, 09:10 AM
  5. using substr
    By riley03 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 06:32 PM