Crash using PreparedStatement [Player.cpp]

I’ll trying make a SELECT from characters table and getting crash on creating new character.

Code

diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp
index 2c0d22b…631efc9 100644
— a/src/server/game/Entities/Player/Player.cpp
+++ b/src/server/game/Entities/Player/Player.cpp
@@ -11501,6 +11501,16 @@ InventoryResult Player::CanEquipItem(uint8 slot, uint16 &dest, Item* pItem, bool
{
TC_LOG_DEBUG(LOG_FILTER_PLAYER_ITEMS, “STORAGE: CanEquipItem slot = %u, item = %u, count = %u”, slot, pItem->GetEntry(), pItem->GetCount());
ItemTemplate const* pProto = pItem->GetTemplate();
+

  •    PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_ITEM_RANK);
    
  •    stmt->setUInt32(0, GetGUID());
    
  •    PreparedQueryResult result = CharacterDatabase.Query(stmt);
    
  •    Field* fields = result->Fetch();
    
  •    uint32 _ItemRank = pProto->ItemRank;
    
  •    uint32 resultItemRank = fields[0].GetUInt32();
    
  •    if(_ItemRank > resultItemRank)
    
  •        return EQUIP_ERR_ITEM_CANT_BE_EQUIPPED;
    
  •    if (pProto)
       {
           // item used
    

diff --git a/src/server/shared/Database/Implementation/CharacterDatabase.cpp b/src/server/shared/Database/Implementation/CharacterDatabase.cpp
index 7e23761…9de3115 100644
— a/src/server/shared/Database/Implementation/CharacterDatabase.cpp
+++ b/src/server/shared/Database/Implementation/CharacterDatabase.cpp
@@ -597,4 +597,5 @@ void CharacterDatabaseConnection::DoPrepareStatements()
PrepareStatement(CHAR_UPD_CHAR_PET_SLOT_BY_ID, “UPDATE character_pet SET slot = ? WHERE owner = ? AND id = ?”, CONNECTION_ASYNC);
PrepareStatement(CHAR_DEL_CHAR_PET_BY_ID, “DELETE FROM character_pet WHERE id = ?”, CONNECTION_ASYNC);
PrepareStatement(CHAR_DEL_CHAR_PET_BY_SLOT, “DELETE FROM character_pet WHERE owner = ? AND (slot = ? OR slot > ?)”, CONNECTION_ASYNC);

  • PrepareStatement(CHAR_SEL_ITEM_RANK, “SELECT ItemRank FROM characters WHERE guid = ?”, CONNECTION_SYNCH);
    }
    diff --git a/src/server/shared/Database/Implementation/CharacterDatabase.h b/src/server/shared/Database/Implementation/CharacterDatabase.h
    index f97dd63…3e00cf2 100644
    — a/src/server/shared/Database/Implementation/CharacterDatabase.h
    +++ b/src/server/shared/Database/Implementation/CharacterDatabase.h
    @@ -528,6 +528,7 @@ enum CharacterDatabaseStatements
    CHAR_SEL_ITEMCONTAINER_MONEY,
    CHAR_DEL_ITEMCONTAINER_MONEY,
    CHAR_INS_ITEMCONTAINER_MONEY,

  • CHAR_SEL_ITEM_RANK,

    MAX_CHARACTERDATABASE_STATEMENTS
    };
    Crash

Revision: TrinityCore rev. 3b71172e8a0f+ 2013-08-29 16:01:48 +0100 (master branch) (Win32, Debug)
Date 18:9:2013. Time 12:13
//=====================================================
*** Hardware ***
Processor: AMD Phenom™ II X2 555 Processor
Number Of Processors: 2
Physical Memory: 4194303 KB (Available: 4194303 KB)
Commit Charge Limit: 4194303 KB

*** Operation System ***
Windows 7 Professional Service Pack 1 (Version 6.1, Build 7601)

//=====================================================
Exception code: C0000005 ACCESS_VIOLATION
Fault address: 01391930 01:00010930 C:ServerCoreworldserver.exe

Registers:
EAX:00000000
EBX:31B7F1F0
ECX:00000000
EDX:00000000
ESI:3B1C9F68
EDI:3B1C9E94
CS:EIP:0023:01391930
SS:ESP:002B:3B1C9DA4 EBP:3B1C9DB0
DS:002B ES:002B FS:0053 GS:002B
Flags:00010212

Call stack:
Address Frame Function SourceFile
01391930 00000000 PreparedResultSet::Fetch+30 c:trinitycoresrcservershareddatabasequeryresult.h line 74
016BB681 00000000 Player::CanEquipItem+F1 c:trinitycoresrcservergameentitiesplayerplayer.cpp line 11508
016BB530 00000000 Player::CanEquipNewItem+60 c:trinitycoresrcservergameentitiesplayerplayer.cpp line 11489
016991BF 00000000 Player::StoreNewItemInBestSlots+8F c:trinitycoresrcservergameentitiesplayerplayer.cpp line 1208
01698EB3 00000000 Player::Create+CD3 c:trinitycoresrcservergameentitiesplayerplayer.cpp line 1156
01C5F1C3 00000000 WorldSession::HandleCharCreateCallback+C33 c:trinitycoresrcservergamehandlerscharacterhandler.cpp line 641
01C5EB52 00000000 WorldSession::HandleCharCreateCallback+5C2 c:trinitycoresrcservergamehandlerscharacterhandler.cpp line 516
0156A634 00000000 WorldSession::ProcessQueryCallbacks+F4 c:trinitycoresrcservergameserverworldsession.cpp line 1093
015675B1 00000000 WorldSession::Update+961 c:trinitycoresrcservergameserverworldsession.cpp line 387
013BD0E6 00000000 World::UpdateSessions+136 c:trinitycoresrcservergameworldworld.cpp line 2615
013BAF74 00000000 World::Update+374 c:trinitycoresrcservergameworldworld.cpp line 1979
0139390A 00000000 WorldRunnable::run+8A c:trinitycoresrcserverworldserverworldthreadworldrunnable.cpp line 61
01E44741 00000000 ACE_Based::thread::ThreadTask+21 c:trinitycoresrcserversharedthreadingthreading.cpp line 186
63E9211D 00000000 ACE_OS_Thread_Adapter::invoke+13D c:trinitycoredepaceliteaceos_thread_adapter.cpp line 103
63DE3EEA 00000000 ace_thread_adapter+2A c:trinitycoredepaceliteacebase_thread_adapter.cpp line 126
63C6A293 00000000 _beginthreadex+243
63C6A224 00000000 _beginthreadex+1D4
760933AA 00000000 BaseThreadInitThunk+12
77B59ED2 00000000 RtlInitializeExceptionChain+63
77B59EA5 00000000 RtlInitializeExceptionChain+36

Local Variables And Parameters

Call stack:
Address Frame Function SourceFile
01391930 00000000 PreparedResultSet::Fetch+30 c:trinitycoresrcservershareddatabasequeryresult.h line 74
punting on symbol this

016BB681 00000000 Player::CanEquipItem+F1 c:trinitycoresrcservergameentitiesplayerplayer.cpp line 11508

I would be very grateful for your help.

Thanks!

This needs to go in the custom code section.

http://www.trinitycore.org/f/forum/9-custom-code-and-requests/

— Canned message start —

This thread is not related to the official Trinity codebase and was moved to the Custom Code section.

Please read the stickies next time.

— Canned message end —

So can anyone tell me about the crash ?

/emoticons/default_mellow.png

Before you do result->Fetch() you need to check if (result), pointer might be NULL

Thanks Machiavelli,

Do not know why I had not thought about it. /emoticons/default_happy.png