Giter Club home page Giter Club logo

Comments (5)

Fischsalat avatar Fischsalat commented on July 22, 2024

Yea, I've seen that issue before and you're right, BlueprintGeneratedClasses need some fixes for StaticClass.

I don't think finding the class every time without any caching is feasible, as most people use StaticClass per-tick to check object-types.

A solutions I am considering is to store the index rather than the pointer and verify the validity of the index of the class every time StaticClass is called.

Pseudo-cpp:

class UClass* ABP_CharacterLightController_C::StaticClass()
{
	static int32 ClassIdx = 0x0;

	/* Could be external function, not really unique to this StaticClass functon */
	static auto SetClassIndex = [](UClass* Object, int32& Index) -> UClass*
	{
		if (Object)
			Index = Object->Index;

		return Object;
	};

	if (ClassIdx == 0x0)
		return SetClassIndex(UObject::FindClassFast("BP_CharacterLightController_C"), ClassIdx);

	UClass* ClassObj = static_cast<UClass*>(UObject::GObjects->GetByIndex(ClassIdx));

	/* Could use cast flags too to save some string comparisons */
	if (!ClassObj || ClassObj->GetName() != "BP_CharacterLightController_C")
		return SetClassIndex(UObject::FindClassFast("BP_CharacterLightController_C"), ClassIdx);

	return ClassObj;
}

from dumper-7.

KulaGGin avatar KulaGGin commented on July 22, 2024

A solutions I am considering is to store the index rather than the pointer and verify the validity of the index of the class every time StaticClass is called.

class UClass* ABP_CharacterLightController_C::StaticClass()
{
	static int32 ClassIdx = 0x0;

	/* Could be external function, not really unique to this StaticClass functon */
	static auto SetClassIndex = [](UClass* Object, int32& Index) -> UClass*
	{
		if (Object)
			Index = Object->Index;

		return Object;
	};

The problem with this is the class object can get destroyed at any point in time, and then once you try to access it, you might be accessing not accessible memory or just garbage memory.

When I was trying to figure out the bugs in the code, once the blueprint class actually gets loaded, the old class object would point to a different object, it wasn't even a class object. And I was just lucky that another object was put at the same memory location as the old class object, so I could get its' name without crashing.
Other times might not be that lucky and once you try to access Object->Index, you might be accessing either inaccessible memory or just garbage reallocated memory, so you can't just check if memory is accessible with IsBadReadPtr(Object): it will be accessible but something else is there now.

I don't think finding the class every time without any caching is feasible, as most people use StaticClass per-tick to check object-types.

Well, that will be on programmers to cache and reset the pointers. I get the class only once at match start and then invalidate on match end, because that's when levels get loaded and unloaded, so it's always correct.

To actually be able to cache these class pointers, you'd need to hook into the object destruction function, and then have like a vector of UClass**(to the fields in each class, so you'd make a field static UClass* StaticClass in each class), and then invalidate those pointers once UClass object gets destroyed ABP_CharacterLightController_C::StaticClass = nullptr, but using the vector of those pointers to the UClass pointers, because you'd be doing it from inside of the object destruction hook. That would fix it, there would be no way to get invalid UClass pointer this way at any point in time, as I understand it.

from dumper-7.

Fischsalat avatar Fischsalat commented on July 22, 2024

The problem with this is the class object can get destroyed at any point in time,

I am aware of this. In the proposed code above I'm explicitly not storing a UClass*, but rather the index of the class from the previous (re-)initialization. Re-fetching the Class from the index will return nullptr if the index was invalidated. If the index is still valid, but the object at said index was changed the check ClassObj->GetName() != "BP_CharacterLightController_C") fails and the object will be re-initialized.

from dumper-7.

Fischsalat avatar Fischsalat commented on July 22, 2024

A fix for the issue has been implemented, but it is mostly untested. It would be nice if you could test the latest commit on main and tell me if StaticClass is still crashing with BP geneated classes.

I'll leave this issue ope until you can confirm it's been fixed.

from dumper-7.

Fischsalat avatar Fischsalat commented on July 22, 2024

As you seemingly don't care about this issues anymore and it has been fixed, I'm going to close this.

from dumper-7.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.