Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unable to setHost because of std::bad_alloc  (Read 4445 times)

0 Members and 1 Guest are viewing this topic.

Blady214

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Unable to setHost because of std::bad_alloc
« on: April 01, 2014, 03:24:39 pm »
Hello,

it's my first steps in SFML, so please be patient. I have prepared SFML libraries for Microsoft Visual Studio 2013 Express which I am using to develop my app. I am trying to send http request but I am unable because of std::bad_alloc error. I know that it is associated with memory management but I am doing everything like in SFML tutorial.

Below is my function that I want to use to create request:
Quote

void apiReseller::openToken()
{
   string requestBody = "api_key=" + apiKey;

   sf::Http httpTokenRequest;
   httpTokenRequest.setHost(apiAddress);

   sf::Http::Request openTokenRequest;
   openTokenRequest.setMethod(sf::Http::Request::Post);
   openTokenRequest.setBody(requestBody);
   openTokenRequest.setUri("/token");

   sf::Http::Response openTokenResponse = httpTokenRequest.sendRequest(openTokenRequest);

   cout << openTokenResponse.getBody();
}

Build process is OK but when program reaches line httpTokenRequest.setHost(apiAddress); it throws an exception std::bad_alloc.

And there is one more thing, when I will set Linker->Generel->Register Output as yes I receive another error when compiling:

Quote
Error   4   error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.   C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets   1620   5   importApiAccounts

Maybe it causes my error with memory?

Please help me if you could, it is second day when I am trying to use this library.

« Last Edit: April 01, 2014, 03:27:54 pm by Blady214 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unable to setHost because of std::bad_alloc
« Reply #1 on: April 01, 2014, 03:27:59 pm »
What's the value of apiAddress, have you checked with the debugger?

string requestBody = "api_key=" + apiKey;
Unless apiKey is a std::string, that's wrong. String concatenation doesn't work like this in C++, you should use string streams or C++11 string conversion functions.
« Last Edit: April 01, 2014, 03:30:27 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #2 on: April 01, 2014, 03:30:25 pm »
Please provide a minimal and complete example that reproduces the error (i.e. put everything into a main function and compile & run it).

What is apiAddress?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Blady214

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #3 on: April 01, 2014, 03:32:58 pm »
apiAddress value is correct. It throws an exception even if I will try to set this address static and type directly, so I am sure that problem is not caused by this variable.

Quote
Unless apiKey is a std::string, that's wrong. String concatenation doesn't work like this in C++, you should use string streams or C++11 string conversion functions.

OK, I will fix it after I will be able to send http request ;)

Blady214

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #4 on: April 01, 2014, 03:36:50 pm »
Please provide a minimal and complete example that reproduces the error (i.e. put everything into a main function and compile & run it).

What is apiAddress?

Below is complete and minimal example, I have put my function and main and delete all variables:

Quote
#include "stdafx.h"
#include <iostream>

#include "SFML\Network.hpp"
#include "SFML\Network\Http.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
   sf::Http httpTokenRequest;
   httpTokenRequest.setHost("https://panel.operolab.pl/api");

   sf::Http::Request openTokenRequest;
   openTokenRequest.setMethod(sf::Http::Request::Post);
   openTokenRequest.setBody("test");
   openTokenRequest.setUri("/token");

   sf::Http::Response openTokenResponse = httpTokenRequest.sendRequest(openTokenRequest);

   cout << openTokenResponse.getBody();
   
   system("PAUSE");
   return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #5 on: April 01, 2014, 03:38:53 pm »
How did you "prepare" SFML for Visual Studio 2013?
Laurent Gomila - SFML developer

Blady214

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #6 on: April 01, 2014, 03:47:07 pm »
How did you "prepare" SFML for Visual Studio 2013?

I have used CMake and prepare sources for Visual Studio and build them in VS for each config (debug, release). After that I have copied dll files into VS directory and link libs and includes into my project. Most I have done from this video:

I have already configured my project only for Debug mode (already I don't release). In attachment is config of my project.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unable to setHost because of std::bad_alloc
« Reply #7 on: April 01, 2014, 03:47:24 pm »
HTTPS is not supported. It should still not crash though.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Blady214

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Unable to setHost because of std::bad_alloc
« Reply #8 on: April 01, 2014, 03:49:55 pm »
I can not use HTTPS? It doesn't work even if I use HTTP.