PDA

View Full Version : [SOLVED] QueryResult inside CPP file crash server?



zhier
07-20-2016, 06:52 PM
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:


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


void WorldSession::HandleBattleFieldPortOpcode(WorldPac ket &recvData)

& Player.cpp


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
07-20-2016, 07:15 PM
You need null checks for "item" and "itemdb" variables. Also look at slp's post below as I didn't catch that.

Example:



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

slp13at420
07-20-2016, 11:43 PM
QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`='%s'", item->GetEntry());


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

%s = string.
%u = number.



QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `items_prohibidos` WHERE `id`='%u';", item->GetEntry());


that can cause a crash.

Tommy
07-20-2016, 11:54 PM
I didn't catch that, rofl. I edited my post and referred yours on that part.

slp13at420
07-21-2016, 12:17 AM
lol I cant even count how many times I made this mistake and crash my core during the query also:



QueryResult itemdb = CharacterDatabase.PQuery("SELECT (`id`) FROM `characters` WHERE `name`='%s';", player->GetName());


when it should be :



QueryResult charid = CharacterDatabase.PQuery("SELECT `id` FROM `characters` WHERE `name`='%s';", player->GetName().c_str());


lol

`!Oh snap forgot to convert it!`

zhier
07-21-2016, 06:17 AM
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!