0

So I've got this interface class that I include, both in the dll and the client project

// InterfaceClass.h 

#pragma once

class InterfaceClass
{
public:
    virtual void Update() = 0;
}; 

This is the dll class that calls one of its own methods inside update

// DLLClassThatDoesSomething.cpp

#include "InterfaceClass.h"
#include <iostream>
#include <string>

class __declspec(dllexport) DLLClass : public InterfaceClass
{
public:
    void Update()
    {
        std::cout << this->GetString();
    }
    std::string& GetString()
    {
        std::string thestring = "bruhmoment";
        return thestring;
    }
};

extern "C"
{
    __declspec(dllexport) InterfaceClass* CreateInstance()
    {
        return new DLLClass();
    }
}

And this is the "Client" project

// main.cpp

#include "InterfaceClass.h"
#include <Windows.h>

typedef InterfaceClass* (__cdecl *Class) ();

int main()
{
    HINSTANCE dll = LoadLibrary(L"DLLClass.dll");

    Class klass = (Class)GetProcAddress(dll, "CreateInstance");

    InterfaceClass* IKlass = klass();

    IKlass->Update();

    FreeLibrary(dll);

    return 0;
}

The moment I call IKlass->Update() I get an exception for Access Memory Violation because of the DLLClass calling its own method. error

I haven't tried anything since I barely know how to load a DLL on runtime and I've used this nifty tutorial

How can I let it call the method and not get thrown an exception? I'm trying to let ppl that will create mods for my game create their own mods with their custom classes for bosses, mobs and etc. in DLLs.

EDIT: Turns out it was a syntax mistake on my end. Instead of return new DLLClass;, it had to be return new DLLClass();. After fixing it, it works as intended.

  • `0xCC` is uninitialized stack memory: [https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Apr 02 '20 at 12:49
  • You should pay attention to compiler warnings, or, even better, build with `/WX` option to halt the build if any warnings occur. In this case VS would definitely warn you about faulty `GetString`. – user7860670 Apr 02 '20 at 13:01
  • ***EDIT: Turns out it was a syntax mistake on my end. Instead of return new DLLClass;, it had to be return new DLLClass();. After fixing it, it works as intended.*** That is not the correct fix. – drescherjm Apr 02 '20 at 18:01

1 Answers1

1

You return a reference to a local variable thestring, and by the time you try to access it in std::cout << this->GetString(), referenced data is already destroyed. In fact, it is destroyed right after the end of enclosing scope of compound statement where the variable was declared.

It may "appear" to work sometimes due to the stack not being overwritten yet, but eventually it will fail miserably like it did in your case. This triggers UB (undefined behavior).

vtronko
  • 478
  • 3
  • 10