Skip to main content
GameDev.net gamedev.net
🔒 Locked

Access Violation Issue

Started by SleepyRhino Jul 27, 2014 at 9:16 AM 2 replies 1.6k views
Original Post
SleepyRhino
SleepyRhino

I am having an issue and can't seem to resolve it.

Here is the code

FontCache.h


#pragma once

#include <SFML/Graphics/Font.hpp>

#include <unordered_map>
#include <memory>
#include <string>

class FontCache
{
public:
	typedef std::shared_ptr<sf::Font> FontPtr;
	typedef std::unordered_map<std::string, FontPtr> FontMap;
	typedef FontMap::iterator FontMapIterator;

public:
	FontCache(unsigned int size = 10);
	~FontCache();
	bool add(std::string name, std::string file);
	void remove(std::string name);
	sf::Font& get(std::string name);
	void unload();

private:
	FontMap resources_;
};

FontCache.cpp


#include "StandardInc.h"
#include "FontCache.h"
#include "PhysFsStream.h"

FontCache::FontCache(unsigned size)
: resources_(size)
{
	
}

FontCache::~FontCache()
{
	unload();
}

bool FontCache::add(std::string name, std::string file)
{
	FontMapIterator it = resources_.find(name);

	if(it != resources_.end())
	{
		return true;
	}

	PhysFsStream stream;
	stream.open(file.c_str());

	FontPtr newFont = std::make_shared<sf::Font>();
	if (!newFont->loadFromStream(stream))
	{
		printf("Error: Unable to load %s\n", file.c_str());
		return false;
	}

	resources_[name] = std::move(newFont);
	printf("Added Font: %s\n", file.c_str());
	return true;
}

void FontCache::remove(std::string name)
{
	FontMapIterator it = resources_.find(name);

	if(it != resources_.end())
	{
		resources_.erase(it);
	}
}

sf::Font& FontCache::get(std::string name)
{
	FontMapIterator it = resources_.find(name);
	if(it == resources_.end())
		add(name, name);

	return *resources_[name];
}

void FontCache::unload()
{
	resources_.clear();
}

Part of GameScene.hpp


    sf::Text buildDetails_ = new sf::Text();
    buildDetails_->setFont(fonts_.get("fonts/PTM55FT.ttf"));
    buildDetails_->setString("Build 0.0.0");
    buildDetails_->setPosition(10, 10);

Whenever I try to use the font with an sf::Text class I get an access violation error, I have tried making a simple version of the code and not using the FontCache class and basically copied and pasted the code from the FontCache class into the GameScene class where the sf::Text is being created and it worked fine.

Any ideas on what might be cause this would be great my mind has gone blank tryign to fix it.

rip-off
rip-off

Have you tried running your code in a debugger? For example, one thing that could be happening is that one of the error paths in FontCache::add is being triggered, but FontCache::get is not checking for such errors.

SleepyRhino
SleepyRhino

Yes the font is added to the map and retrieved from the map fine.

Seems like the error is being thrown from within the SFML graphics dll.

So I have built SFML so I could get the pdb files to do abit of deeper digging.

At this call in the Font::loadGlyph is where he trouble starts


    // Load the glyph corresponding to the code point
    if (FT_Load_Char(face, codePoint, FT_LOAD_TARGET_NORMAL | FT_LOAD_FORCE_AUTOHINT) != 0)
        return glyph;

And the if (static_cast(stream->seek(offset)) == offset) line is where is fails.


    // FreeType callbacks that operate on a sf::InputStream
    unsigned long read(FT_Stream rec, unsigned long offset, unsigned char* buffer, unsigned long count)
    {
        sf::InputStream* stream = static_cast<sf::InputStream*>(rec->descriptor.pointer);
        if (static_cast<unsigned long>(stream->seek(offset)) == offset)
        {
            if (count > 0)
                return static_cast<unsigned long>(stream->read(reinterpret_cast<char*>(buffer), count));
            else
                return 0;
        }
        else
            return count > 0 ? 0 : 1; // error code is 0 if we're reading, or nonzero if we're seeking
    }

So when SFML is checking the Font pointer is it fine and can access it but when it gets to here I get an Access violation error.

ChaosEngine
ChaosEngine




And the if (static_cast(stream->seek(offset)) == offset) line is where is fails.

Is stream a valid pointer at that point? You should be able to look at it in the debugger and see some sensible values for each member.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.