AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   About CommitSuicide (https://forums.alliedmods.net/showthread.php?t=183813)

Yeah=} 04-27-2012 06:04

About CommitSuicide
 
Hello, I have a question with very strange behaviour of code. So, I have a function that causes a crash:

Code:

void CPlugin::FireGameEvent( IGameEvent *event )
{
    std::string name = event->GetName();

    g_SMAPI->ConPrint( ( name + " Just called\n" ).c_str( ) );

    if( name == "player_say" )
    {
        std::string msg = event->GetString( "text" );

        edict_t* pEdict = DrUtils::GetPEdictFromUserId( event->GetInt( "userid" ) );

        if( pEdict != nullptr )
        {
            CBasePlayer* pBase = reinterpret_cast< CBasePlayer* >( pEdict->GetUnknown( )->GetBaseEntity( ) );

            if( pBase != nullptr )
            {
                CommitSuicide( pBase );
            }
            else
            {
                g_SMAPI->ConPrint( "Culd not get CBasePlayer\n" );
            }
        }
        else
        {
            g_SMAPI->ConPrint( "Could not get edict_t" );
        }
    }
}

When I comment this line
Code:

std::string msg = event->GetString( "text" );
it works, however I think that problem is not in this line because then I tried to comment this line
Code:

CommitSuicide( pBase );
and it worked ok. WTF I'm doing wrong? :cry:
PS. Code of CommitSuicide

Code:

CommitSuicide( CBaseEntity* pEntity  )
        {
            void **this_ptr = *(void ***)&pEntity;
            void **vtable = *(void ***)pEntity;
            void *func = vtable[433];

            union {    CBaseEntity *(EmptyClass::*mfpnew)( );
            void *addr;    } u;    u.addr = func;

            (reinterpret_cast<EmptyClass*>(this_ptr)->*u.mfpnew)( );
        }


Powerlord 04-27-2012 09:20

Re: About CommitSuicide
 
My first thought is this:

event->GetString returns a const char* not a std::string.

To my knowledge, the STL isn't supported in SRCDS at all.

asherkin 04-27-2012 09:39

Re: About CommitSuicide
 
Quote:

Originally Posted by Powerlord (Post 1697365)
event->GetString returns a const char* not a std::string.

There is an overload on the assignment operator to make it work.

Quote:

Originally Posted by Powerlord (Post 1697365)
To my knowledge, the STL isn't supported in SRCDS at all.

While you can technically use it, the reason not to is if you need to support Linux systems other than the one it was compiled on, due to terrible ABI compatibility between versions, and Valve's own usage.

One major issue is that you're not checking if event is not NULL.

Yeah=} 04-27-2012 09:53

Re: About CommitSuicide
 
Quote:

Originally Posted by asherkin (Post 1697372)
One major issue is that you're not checking if event is not NULL.

Hmm, I've added
Code:

if( !event )
    {
        return;
    }

on the top of the function. Still not working...

PS. Now I have this code: http://pastebin.com/6PtmPTjB and it doesn't work. But it works ok http://pastebin.com/H5v9QXPt. This code also dont crash server http://pastebin.com/0bTpTSYE.
take note of the commented line :)


All times are GMT -4. The time now is 13:09.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.