c++ dbgheap.c error

Started by
2 comments, last by BaneTrapper 10 years, 8 months ago

Hello.

I encountered exception ( dbgheap.c at line 1322, Expression: _CrtIsValidHeapPointer(pUserData) ) this error when debuging it pops up after this function, (Guessing some object is deleting and is causing issue)


void Inventory::PickUpItem(int vecPos, en::PosInvQS place1)
{
    std::cout<<"PickUpFunc"<<std::endl;

    std::auto_ptr<Item> APtempI;
    if(place1 == en::PosInvQS::Inventory) APtempI.reset(&itemInventoryList[vecPos]);
    else if(place1 == en::PosInvQS::QuickSlot) APtempI.reset(&itemQuickslotList[vecPos]);
    else std::cout<<"Error at Picking up item"<<std::endl;

    manipulatedItemInvPos = APtempI->position;//save position
    APtempI->position = en::ItemPosition::mouse; //set new position
    manipulatedItemVecPos = vecPos;//save vectorID
    isItemPickedUp = true; // There is a item being manipulated
    manipulatedItemPosInvQS = place1;
}

Any suggestions or what should i do to get this error fixed would be greatly appreciated!

Also is there a way to make a "drop down box" i mean that i can put a 1k char code here without making the post visible until user clicks "show" or "open"...

If you require complete code i am willing to post it, just let me know.

Advertisement

std::auto_ptr deletes its pointer when it's destroyed, but the pointer you assign to it does not appear to be allocated with new.

Looks like it is an element owned by std::vector, so that is never safe to delete. Probably should just be a raw pointer, or potentially a reference (you need to assign on declaration which is not ideal there).

While smart pointers are said to be the correct solution for memory management a lot, if a piece of code does not own the memory (either exclusive, e.g. auto_ptr or unique_ptr, or shared e.g. shared_ptr, weak_ptr or various intrusive reference counting schemes), then a raw pointer or reference is fine

EDIT:

Somewhat unrelated to your question. for my inventory implementations that had "quick slots", I did not actually go to the effort of making the quick slots special for the inventory in some way. Just that things that use the inventory, give some special meaning to the first n slots (e.g. for me the first 10 slots of the player is rendered by the hud, and can be selected as the "active" slot which is again, something handled by the player not the inventory), and keeps things simpler for all the stuff that doesn't have or care about quick slots or active slots or whatever, the basic Inventory is literally just a container around an array.

std::auto_ptr deletes its pointer when it's destroyed, but the pointer you assign to it does not appear to be allocated with new.

OH that makes so much sense now.

Right... its deleting a member in middle of vector and that's issue...

Wrong pointer i guessing... I don't want to delete the stored data when the object is deleted

EDIT:

Somewhat unrelated to your question. for my inventory implementations that had "quick slots", I did not actually go to the effort of making the quick slots special for the inventory in some way. Just that things that use the inventory, give some special meaning to the first n slots (e.g. for me the first 10 slots of the player is rendered by the hud, and can be selected as the "active" slot which is again, something handled by the player not the inventory), and keeps things simpler for all the stuff that doesn't have or care about quick slots or active slots or whatever, the basic Inventory is literally just a container around an array.

Well i split it up because the user controls player that has (inventory + quickslots) npc have only (inventory) and use items from there so i don't have to mess around when i don't have to... the user wont feel the difference.

Quick slots at start are (2) and can go up to (11). 1=left hand 2 = right hand.

Additional slots can be gained by equipping items like "Leather pouch" that will add 1-3 slots for "Small type" of item like potion, gold, keys...

Still thinking how to hotkey that up... that is the main reason for quick bar.

EDIT::
I don't know why i used auto_ptr there, a normal pointer would suffice and provide same equivalent.

I was trying to prevent memory leak when there was none...

This topic is closed to new replies.

Advertisement