SFML community forums

Help => Network => Topic started by: Blady214 on April 01, 2014, 03:24:39 pm

Title: Unable to setHost because of std::bad_alloc
Post by: Blady214 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.

Title: Re: Unable to setHost because of std::bad_alloc
Post by: Nexus 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.
Title: Re: Unable to setHost because of std::bad_alloc
Post by: eXpl0it3r 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?
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Blady214 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 ;)
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Blady214 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;
}
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Laurent on April 01, 2014, 03:38:53 pm
How did you "prepare" SFML for Visual Studio 2013?
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Blady214 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: https://www.youtube.com/watch?v=VcPRJE6Rt0o

I have already configured my project only for Debug mode (already I don't release). In attachment is config of my project.
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Nexus on April 01, 2014, 03:47:24 pm
HTTPS is not supported. It should still not crash though.
Title: Re: Unable to setHost because of std::bad_alloc
Post by: Blady214 on April 01, 2014, 03:49:55 pm
I can not use HTTPS? It doesn't work even if I use HTTP.