[Solved] Read access violations in win32 project, which don't happen in win10 project

Read access violations in win32 project, which don’t happen in win10 project

I was working on a game, and I put the code in the win10 solution. I later found out that to build it for win32 (or windows at all), I had to move my work to the win32 solution. So I did, and managed to make a release build through the cocos console.

However, when running the exe in the output folder, the game crashed before the game window even popped up. Using the debugger in the win32 project, apparently there was a read access violation in one part of the code, where it worked just fine in the win10 project.

The read access violation changes depending on whether I run the game through the Release config or the Debug config. They both point to different lines in the code, within the same function.

Here’s the snippet:

void ScoreLabel::init(string& name)
{
	this->name = name;
	score = 0; // <- the crash happens here in the Debug config.
	previousScore = score;
	string text = name + ": " + to_string(score); // <- the crash happens here in the Release config
	setString(text);
}

How do I fix this? Are there perhaps settings I’m unaware of that are causing this? xD

I’m using cocos2d-x version 3.15.1 on Windows 10.

Solved! See here.

This code may not work cross-platform, as per my previous experience.
Do this,
string text = StringUtils::format("%s: %d", name.c_str(), score);

What do you mean it’s not possible in c++? Also, I tried your solution, and the crash happened again. In the same place in the Debug configuration, but in the Release config, it happened here in this snippet in main.cpp:

// create the application instance
AppDelegate app;
return Application::getInstance()->run(); //<- crash happened here

Not true! Didn’t you even bother to google it before stating that it is not possible? Have you heard about such thing as C++11? http://www.cplusplus.com/reference/string/to_string/ BTW, before crashing with read access violation the C++ code should be compiled, which is definitely “not possible” if there is something “not possible in C++” in that code! :joy:

Can you please provide me with ScoreLabel.h header file? I’m sure that will help me to point to the issue.

Here you go :slight_smile:

#pragma once

#include <string>
#include "cocos2d.h"

class ScoreLabel : public cocos2d::Label
{
public:
	ScoreLabel(std::string& name);

	void setScore(int newScore);

	static ScoreLabel* createWithTTF(std::string& name, std::string& fontFilePath, float fontSize,
									const cocos2d::Size& dimensions = cocos2d::Size::ZERO,
									cocos2d::TextHAlignment hAlignment = cocos2d::TextHAlignment::LEFT,
									cocos2d::TextVAlignment vAlignment = cocos2d::TextVAlignment::TOP);

	void update(float dt) override;

private:
	void init(std::string& name);
	int score;
	int previousScore;
	std::string name; // name of who or what the score belongs to


};

Sorry, i written that in hurry. And i am wrong. I didnot mean to say that But,
I had problem with that earlier in Win32 or Android and by using StringUtils its working fine for me.

So problem is somewhere else.

My assumption is that the issue is most likely in “std::string& name”. Try to change it from reference (std::string&) to value (std::string) in all functions. Maybe, when void ScoreLabel::init(string& name) is called, the variable at which the reference is pointing is not in memory anymore. Also you can double check with debugger when ScoreLabel’s ctor and init(…) are called. It could be a mess there as static *type createWithSomething(…) + init(…) is a bit tricky technique. If it doesn’t help then I really don’t know what’s wrong with this code. :face_with_raised_eyebrow:

BTW, I advise you to use const reference instead of just reference. And always be aware that reference is not a value, so you should care about the initial variable which reference points to!

No problem! C++11 is really cool! :cowboy_hat_face:

Thanks for the help, everyone. :slight_smile: It turns out the real issue was in the way I designed the ScoreLabel class; I was trying to instantiate ScoreLabels by static_casting regular Labels to ScoreLabels (in CreateWithTTF, supplemented by init), and apparently that messed up the memory. And thus it’s strange how it worked in the win10 project at all xD

So I did a little redesign, and now it works perfectly! Whether it’s building the game in Debug or Release, or deploying it through the cocos console, it works as it should! :slight_smile:

Credit to @Nuno147 for finding and showing me the issue.