my program seems good on Windows,while on Ubuntu it is crashed - qt

I'm programing a plane game on Windows by QT which seems good,while on Ubuntu it is crashed.It has a QLinkedList<Bullet *> Bullet_list.and it is crashed after Bullet_list.removeOne(*ite)
I've tried judging every Null-pointer,adding delete *ite after removeOne(),but it doesn't work.
Plane_Player *Plane_p;
connect(timer,SIGNAL(timeout()),this,SLOT(Refresh()));
void GameWindow::Refresh(){
QLinkedList<Bullet*>::iterator ite;
for(ite = Plane_p->Bullet_list.begin();ite != Plane_p->Bullet_list.end();++ite)
{
(*ite)->Go();//move the bullets on the screen
if((*ite)->isOutOfBound())
Plane_p->Bullet_list.removeOne(*ite);//crashed after this.
}
}
class Plane_Player
{
public:
QLinkedList<Bullet *> Bullet_list;
}
in the function Refresh(),there is other removeOne(),and it's all crashed after it.
I hope it won't be crashed on Ubuntu.
the whole project:https://github.com/Vinolzy/QT

Qt documentation states that:
If you want to insert, modify, or remove items in the middle of the
list, you must use an iterator.
So far so good. But I think the problem is that you reuse the iterator after removing an element from your linked list. To fix it you need to update your iterator after removal. For example, you can rewrite your loop like:
QLinkedList<Bullet*>::iterator ite = Plane_p->Bullet_list.begin();
while (ite != Plane_p->Bullet_list.end())
{
(*ite)->Go();
if ((*ite)->isOutOfBound())
{
ite = Plane_p->Bullet_list.erase(ite);
}
else
{
++ite;
}
}
And finally, it works differently on different platforms because your code leads to an undefined behavior.

Related

How i can to raise QFileDialog to the foreground if functions raise(),activeWindow() and other didn't work?

I'm using go-qt bindings (therecipe).
I faced such a problem that I cannot bring the window with the file dialog forward, I tried all the functions (and their combinations) that I could find on the Internet, but none of them did not help bring the dialog up.
I try to use this function:
fileDialog.SetWindowFlag(core.Qt__WindowStaysOnTopHint,true)
fileDialog.ActivateWindow()
fileDialog.SetWindowState(core.Qt__WindowActive)
fileDialog.SetWindowState(core.Qt__WindowMinimized|core.Qt__WindowActive)
fileDialog.Raise()
fileDialog.SetFocus2()
I also noticed a feature that if you call the dialog again after fileDialog.Exec (), then it will be displayed on top of all windows as needed.
code for this case
var fileDialog = widgets.NewQFileDialog2(nil, "Open Directory", "", "")
if fileDialog.Exec() != int(widgets.QDialog__Accepted) {
return
}
if fileDialog.Exec() != int(widgets.QDialog__Accepted) {
return
}
Code for function where I'm using Dialog:
func choseFile(){
var fileDialog = widgets.NewQFileDialog2(nil, "Open Directory", "", "")
fileDialog.SetAcceptMode(widgets.QFileDialog__AcceptOpen)
fileDialog.SetFileMode(widgets.QFileDialog__ExistingFile)
fileDialog.SetWindowFlag(core.Qt__WindowStaysOnTopHint,true)
if fileDialog.Exec() != int(widgets.QDialog__Accepted) {
return
}
fmt.Println(fileDialog.SelectedFiles()[0])
}
Problem might be related to native dialogs (in my case I am using ubuntu), so I put the flag DontUseNativeDialog. Then the problem was solved.
filename := widgets.QFileDialog_GetOpenFileName(ac.MainWindow,"Open Directory","","","",widgets.QFileDialog__DontUseNativeDialog)
upd: Its works even if the first argument is nil.

How to suitably avoid RangeErrors when "looking around" this 2D array?

I have a 2D array structure to represent a grid of tiles that is a part of the game I am making. One aspect of the game is that the grid is filled in in a somewhat random fashion, based on analysis of a text file. Right from the outset though, I already realised that just leaving it be pretty much randomly done like this without sticking in some kind of validity checks or prevention mechanism, to stop really badly configured grid from forming, would not work out. The main problem I want to avoid is too many tiles that would be untraversable being close together, potentially severing chunks of the grid from the rest.
The idea I came up with to try avoid some really bad grids is to check when assigning a tile value to each "grid square" during generation with logic like this
if (tileBeingInserted.isTraversable()) {
//all is well
return true;
} else {
//we may have a problem, are there too many untraversables nearby?
//Proceed to check all squares "around" the current one.
}
To be clear, checking around the current square means checking the square immediately adjacent in each of the 8 cardinal directions. Now, my problem is that I am trying to reason out how to code this so that it will certainly not give a RangeErrorat any point or at least catch it and recover if it must. As an example, you could clearly take one of the corner squares to be the worst scenario in the sense that only 2 of the squares the algorithm would want to check are within the array's bounds. Naturally, if a RangeErrorhappens for this reason I just want the program to progress onward without issue so the structure
try {
//check1
//check2...8
} catch (RangeError e) {
}
is unacceptable because as soon as a single out of range square is tested the code falls out of the check block. An alternative I thought of, but do not like because of its messiness, would be to individually wrap each check in a try-catch and yes that would work I guess but that's some horrid looking code...so can anyone help me out here? Is there perhaps a different angle from which to come at this problem of avoiding the RangeErrors that I am not seeing?
So my code for testing whether another untraversable tile should be placed has shaped up like this:
bool _tileFitsWell(int tileTypeInt, int row, int col)
{
//...initialise some things, set stuff up
...
if (tile.traversable == true) {
//In this case a new traversable tile is being put in, so no problems.
return true;
} else {
//begin testing what tiles are around the current tile
//Test NW adjacent
if (row > 0 && col > 0) {
temp = tileAt(row - 1, col - 1);
if (!temp.traversable) {
strikeCount++;
}
}
//Test N adjacent
if (row > 0) {
temp = tileAt(row - 1, col - 1);
if (!temp.traversable) {
strikeCount++;
}
}
//Test NE adjacent
if (row > 0 && col < _grid[0].length - 2) {
temp = tileAt(row - 1, col 1);
if (!temp.traversable) {
strikeCount++;
}
}
//Test W adjacent
if (col > 0) {
temp = tileAt(row, col - 1);
if (!temp.traversable) {
strikeCount++;
}
}
}
return strikeCount < 2;
}
The code inside each "initial" if-statement (the ones that check row and col) is a bit pseudocode-ish for simplicity's sake. As I explained in a previous comment, the reason why I don't need to check tiles in the other 4 cardinal directions is since these checks are done while filling the map, tiles in those positions will always be either uninitialised or just out of bounds, depending on what tile the function is called to check at a given time.

Intercept rdtsc instruction from guest vm userspace in KVM

I'm stuck in the problem as the title says.I want to do this in VMM by adding
the CPU_BASED_RDTSC_EXITING flag in vmx.c(arch/x86/kvm) in setup_vmcs_config function,and
then handle the vm_exit by myself(ref this:mail list).The question is that I cannot
tell whether the vm_exit of rdtsc is caused by the guest kernel or the guest vm user
space application, the latter one is what exactly I want to intercept.I have tried to search
through qemu-kvm-1.2.0 src to find other ways to intercept the rdtsc instruction,I find
rdtsc clue in target-i386/translate.c.And I add a printf there, but I got nothing.So I
wonder if anyone could give me some little guidance to break through.Thank you a lot~
After some experiment,I almost find the answer to my own question.See the code below:
static int handle_rdtsc(struct kvm_vcpu *vcpu)
{
u64 data;
if (vmx_get_msr(vcpu, MSR_IA32_TSC, &data)) {
kvm_inject_gp(vcpu, 0);
printk("wsh_handle_rdtsc_return\n");
return 1;
}
vcpu->run->exit_reason = 20;
vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
skip_emulated_instruction(vcpu);
if(vmx_get_cpl(vcpu)>0)
{
printk("wsh_handle_rdtsc,cpl:%d\n",vmx_get_cpl(vcpu));
}
return 1;
}
As you have seen above,I use vmx_get_cpl to filter those rdtsc vm_exit caused by the guest kernel,and I fact,the printk always print 3,because the privellege level of guest vm applications is always 3.Any corrections is welcomed!

box2d CreateFixture with b2FixtureDef gives pure virtual function call

i have this code that gives me run time error in the line :
body->CreateFixture(&boxDef)
im using cocos2d-x 2.1.5 with box2d 2.2.1 in windows
CCSprite *sprite = CCSprite::create(imageName.c_str());
this->addChild(sprite,1);
b2BodyDef bodyDef;
bodyDef.type = isStatic?b2_staticBody:b2_dynamicBody;
bodyDef.position.Set((position.x+sprite->getContentSize().width/2.0f)/PTM_RATIO,
(position.y+sprite->getContentSize().height/2.0f)/PTM_RATIO);
bodyDef.angle = CC_DEGREES_TO_RADIANS(rotation);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
b2FixtureDef boxDef;
if (isCircle)
{
b2CircleShape circle;
circle.m_radius = sprite->getContentSize().width/2.0f/PTM_RATIO;
boxDef.shape = &circle;
}
else
{
b2PolygonShape box;
box.SetAsBox(sprite->getContentSize().width/2.0f/PTM_RATIO, sprite->getContentSize().height/2.0f/PTM_RATIO);
boxDef.shape = &box;
}
if (isEnemy)
{
boxDef.userData = (void*)1;
enemies->insert(body);
}
boxDef.density = 0.5f;
body->CreateFixture(&boxDef) //<-- HERE IS THE RUN TIME ERROR
;
when i debug the box2d code im getting to b2Fixture.cpp
in the method :
void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
in the line :
m_shape = def->shape->Clone(allocator);
getting the runtime error :
R6025 pure virtual function call
Tricky one. I ran into this myself a couple times. It has to do with variable scope.
The boxDef.shape is the problem. You create the shapes as local variables in the if/else blocks and then assign them to boxDef. As soon as execution leaves the if/else block scope those local variables will be garbage. The boxDef.shape now points to freed memory.
The solution is to keep the shape variables in scope by moving the circle and box shape declarations before the if/else block.

ConcurrentModificationError on Vector

What I am trying to accomplish here is to remove a "blossom" from the Vector whenever a collision is detected. However, I keep getting a ConcurrentModificationError. It messes up when I try to remove the blossom from the Vector. I have tried doing it many ways. At one point when it was detected that the blossom should be removed, I saved its position in the Vector and then tried to remove it when the next position in the list was being looked at. I think this is the only method that you need to see. Can anybody see what I can do to fix this??
private synchronized void DrawBlossoms(Canvas c) // method to draw flowers on screen and test for collision
{
Canvas canvas = c;
for(Blossom blossom: blossomVector)
{
blossom.Draw(canvas);
if (blossom.hit(box_x,box_y, box_x + boxWidth, box_y + boxHeight, blossomVector) == true)
{
Log.v(TAG, "REMOVE THIS!");
//blossomVector.remove(blossom);
}
}
}
The solution is to use an iterator and synchronize on the Vector.
synchronize(blossomVector)
{
Iterator dataIterator = blossomVector.iterator();
while (dataIterator.hasNext())
{
//... do your stuff here and use dataIterator.remove()
}
}

Resources