• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[SOLVED] QueryResult inside CPP file crash server?

Status
Not open for further replies.

zhier

Member
Hey, I'm doing a new system and it requires a Query consult to avoid compile every time I need to add a new ID to the array, so I made a db and I'm getting the data.

I write:

Code:
Item* item = _player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`='%s'", item->GetEntry());

Inside: BattlegrounHandler.cpp

Code:
void WorldSession::HandleBattleFieldPortOpcode(WorldPacket &recvData)

& Player.cpp

Code:
InventoryResult Player::CanEquipItem(uint8 slot, uint16 &dest, Item* pItem, bool swap, bool not_loading) const

BUT!!... When I start the server (it compiles good), I access to my acc and if I wanna create a new player or access to the world with my curren player, Trinity WorldServer.exe crashes.

(Yes, If a delete this code, WorldServer don't crash).

Can u help me? Thanks
 

Tommy

Founder
You need null checks for "item" and "itemdb" variables. Also look at slp's post below as I didn't catch that.

Example:

Code:
Item* item = _player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
[COLOR="#00FF00"]if (item) // If item exists (not null)
{[/COLOR]
    [COLOR="#00FF00"]// We can continue our code[/COLOR]
    QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`='%u'", item->GetEntry());
[COLOR="#00FF00"]    if (itemdb) // If our query successfully ran (not null)
    {
        // We can continue to do whatever here now
    }[/COLOR]
[COLOR="#00FF00"]}[/COLOR]
 

slp13at420

Mad Scientist
Code:
QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`=[COLOR="#FF0000"]'%s'[/COLOR]", item->[COLOR="#FFD700"][/COLOR][COLOR="#FF0000"]GetEntry[/COLOR]());

item->GetEntry() will return a uint32 value not a string value.

%s = string.
%u = number.

Code:
QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`=[COLOR="#FFD700"]'%u'[/COLOR];", item->GetEntry());

that can cause a crash.
 

slp13at420

Mad Scientist
lol I cant even count how many times I made this mistake and crash my core during the query also:
Code:
[COLOR="#808080"]
QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `characters` WHERE `name`='%s';", player->GetName());[/COLOR]

when it should be :
Code:
[COLOR="#808080"]
QueryResult charid = CharacterDatabase.PQuery("SELECT `id` FROM `characters` WHERE `name`='%s';", player->GetName()[COLOR="#DAA520"].c_str()[/COLOR]);[/COLOR]

lol

`!Oh snap forgot to convert it!`
 

zhier

Member
Omg Haha what a pity...

Thanks a lot, it works.

I didn't knew that about %s and %u.

This will help me for the rest of my life.

Solved!
 
Status
Not open for further replies.
Top