Std::string crash editor

Hello,

Unreal engine crash when i’m manipulating a string. I don’t have any compilation error and this code works outside of unreal engine(I did ALOT of test). If it’s possible, I don’t want to use FString(I don’t like it).I’m using the 4.8.3 version of unreal. Here some code where my engine crash.

void	TwitClient::generateNonceTimestamp()
{
	char szTime[32];
	char szRand[32];
	static char	Alphabet[63] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	memset(szTime, 0, 32);
	memset(szRand, 0, 32);
	sprintf_s(szRand, "%x", Alphabet[rand() % 63]);
	sprintf_s(szTime, "%ld", time(NULL));

	oauth_nonce.assign(szTime); // CRASH HERE
	oauth_nonce.append(szRand);
	while (oauth_nonce.size() < 32)
	{
		sprintf_s(szRand, "%x", Alphabet[rand() % 63]);
		oauth_nonce += szRand;
	}
	if (oauth_nonce.size() > 32)
		oauth_nonce.erase(32, std::string::npos);
	oauth_timeStamp = szTime;
}

And my class:

#pragma once

#include <map>
#include <string>
#include "Http.h"

/**
 * 
 */

class TWITHUNTER_API TwitClient : public UObject
{
public:
	TwitClient();
	~TwitClient();
	bool	initialize();
	void	oauthRequestToken();
	void	oauthAccessToken(std::string const&, std::string const&, bool const);
	void	oauthAuthorize();
	void	getHomeTimeline();
	void	retweetToId(std::string const&);
	void	favoriteId(std::string const&);
	void	logOut();
	bool	getHeader(std::string const&, std::map<std::string, std::string>, std::string const&, std::string&);
	void	generateNonceTimestamp();
	void	createSignature(std::map<std::string, std::string>, std::string const&, std::string const&, std::string&);
	void	OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
	std::string	const& getMessageBody() const { return _messagebody; }

	std::string		char2hex(char dec);
	std::string		urlencode(const std::string &c);
private:
	std::string	oauth_consumer_key;
	std::string oauth_consumer_secret;
	std::string	oauth_nonce;
	std::string oauth_timeStamp;
	std::string oauth_signature_method;
	std::string oauth_version;
	std::string oauth_tokenKey;
	std::string oauth_tokenSecret;
	std::string oauth_pin;
	std::string oauth_callback;
	std::string _password;
	std::string _username;
	FHttpModule	*Http;

	std::string _messagebody;

};

Thanks in advance.

Hello,

I am sorry to hear about your issue.

Please note that Unreal Engine 4 provides some additional functionality comparing to pure C++, such as Reflection, which in turn provides Garbage Collection.

However, this requires some additional steps. Engine classes are designed to make the workflow easier and minimize the effort of the developer.

Since std::string doesn’t provide necessary UObject-related features, it is likely to be error-prone.

Thus, please use Engine classes, such as FString. If it does not satisfy some specific requirements, please note that there are other classes in UE4 that deal with text and should be helpful for you, such as FName and FText. If you like to learn more about them, please go here:

Hope this helped!

Good luck!

Hi,

Thanks for your fast response.

The reason of my problem was not std::string, but the declaration of my UObject TwitClient:

Before
void ASpawnTweet::BeginPlay()
{
	Super::BeginPlay();

	PrimaryActorTick.bCanEverTick = true;

	_tweet = new UTwitClient;
	if (_tweet)
	{
		_tweet->initialize();
		getHomeTimeline();
	}
}

After

void ASpawnTweet::BeginPlay()
{
	Super::BeginPlay();
PrimaryActorTick.bCanEverTick = true;

_tweet = NewObject<UTwitClient>();
if (_tweet)
{
	_tweet->initialize();
	getHomeTimeline();
}

}

Thanks for the documentation I’ll have a look at it.