Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Sloved] On deleting an image - GL_INVALID_OPERATION  (Read 3514 times)

0 Members and 2 Guests are viewing this topic.

Andy

  • Newbie
  • *
  • Posts: 18
    • View Profile
[Sloved] On deleting an image - GL_INVALID_OPERATION
« on: November 18, 2010, 11:14:03 pm »
I'm gonna get laughed at, but what the heck...

When an object dynamically allocates an sf::image and then deletes it, I get the following error:
Code: [Select]

An internal OpenGL call failed in Image.cpp (769) : GL_INVALID_OPERATION, the specified operation is not allowed in the current state


Posting the whole code wouldn't really be feasible, so I'll post the relevant stuff.

Object using the Wrapper Object,
please note that Image is in this case is not the sf::Image, but a "wrapper-class":
Code: [Select]

rect = _rect;
img.remake(rect.toSize(), clr);

        Rectangle destBar(2, 2, rect.w - 2, rect.h - 2);
/// background
        Image(*api, Size(rect.w - 4, 20), Color(0x00, 0x20, 0x60)).copyImageTo(img, destBar);
title.location = PointF((float)rect.x + 4, (float)rect.y + 4);
/// title seperator

Image(*api, Size(rect.w - 6, rect.h - 27), Color(0x00, 0xB0, 0xF0)).copyImageTo(img, Rectangle(3, 24, rect.w - 4, rect.h - 27));


code from the "wrapper-class"
Code: [Select]

Image::~Image(){
if (imageHandle){
std::cout << "info: " << info << std::endl << toString() << std::endl;
api->deleteImage(this);
std::cout << "image dtor called\n";
}
}
void Image::loadImage(const char * filename){
api->loadImage(this, filename);
info = filename;
}


and finally the code from the api-plugin:
loading an Image:
Code: [Select]

    void PCOUT loadImage(fsa::Image * image, const char * filename){
        debugReport << "Atempting to load " << filename << std::endl;
        bool sucess;
if (image->imageHandle)
deleteImage(image);
image->imageHandle = new sf::Image;
        sucess = image->imageHandle->LoadFromFile(filename);
        if (sucess){
            //masterFrames.push_back(newFrame);
            //image->imageHandle->LoadFromFile(filename) = &masterFrames.at(masterFrames.size() - 1);
            debugReport << "Loaded to address: " << image->imageHandle << std::endl;
        } else {
/// image has value, won't get here without allocation taking place for the image
deleteImage(image);
            debugReport << "Cannot load image: " << filename << std::endl;
        }
    }

the blank image, other images seem ok but deleting images this function generates seems to cause the error.
Code: [Select]

void PCOUT blankImage(fsa::Image * img, const fsaHelps::Size & sz, const fsaHelps::Color & clr){
if (img->imageHandle)
deleteImage(img);
debugReport << "Making blank image size of " << sz.toString() << " with color " << clr.toString() << std::endl;
img->imageHandle = new sf::Image(fsaHelps::fsa_abs(sz.w), fsaHelps::fsa_abs(sz.h), *(sf::Color *)(&clr));
}

deleting an Image:
Code: [Select]

    void PCOUT deleteImage(fsa::Image * image){
        debugReport << "Deleting Image\n";
delete image->imageHandle;
    }


Is the problem visible? Being s--- code that it is, please send your criticisms (It helps :P).
How should I fix this error?

Lemme know if you need access to the full source (GPLv3/LGPLv3 w/ static exception).

Thanks for your time, your criticisms and any ideas to fix this issue.
-Andy

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Sloved] On deleting an image - GL_INVALID_OPERATION
« Reply #1 on: November 19, 2010, 07:57:44 am »
Are multiple threads involved? Does the error happen at global exit, or even when you delete images while the app is running?

I think it would be easier for you to write a complete and minimal source code that reproduces the problem, from scratch. Debugging separate pieces of your whole application can be very hard, and I can't test them myself.
Laurent Gomila - SFML developer

Andy

  • Newbie
  • *
  • Posts: 18
    • View Profile
[Sloved] On deleting an image - GL_INVALID_OPERATION
« Reply #2 on: November 19, 2010, 07:47:43 pm »
Thanks for the response.

This application as a whole is just one thread. The error occurs during the global exit. Not all the dtors involved seems to be called (using a cout in the dtor to determine this).

I'll get a minimal code deal worked up later. I've got a lot to take care of atm.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Sloved] On deleting an image - GL_INVALID_OPERATION
« Reply #3 on: November 19, 2010, 07:59:58 pm »
Quote
Not all the dtors involved seems to be called (using a cout in the dtor to determine this).

Don't forget that std::cout is a global object too, and it may be destroyed before some of your objects.
Laurent Gomila - SFML developer

Andy

  • Newbie
  • *
  • Posts: 18
    • View Profile
[Sloved] On deleting an image - GL_INVALID_OPERATION
« Reply #4 on: November 19, 2010, 08:51:03 pm »
Your (Mr. Gomila) sf::Image and sf::Font classes don't like anonymous namespaces. (Curious to know why, but anything I would say would just be speculation.)

relevant code:
Code: [Select]

#include "apis.h"
using namespace
namespace{
    sf::Image test;
}
void run(){
    test.LoadFromFile("ball.png");
    .... run app code ....
}


I've encountered it before, however this was under a different context so I didn't catch it.

I'm thinking of just using a pointer in the anon namespace to the vector of Images, so the dtors for these classes aren't called in such a weird way (out of context).

Rather anti-climatic I'm afraid, but I'd say that the simply the social interaction helped me think of this angle for solving the issue.

Thanks for your patience.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Sloved] On deleting an image - GL_INVALID_OPERATION
« Reply #5 on: November 19, 2010, 11:33:27 pm »
You shouldn't use such classes at global scope. They own OpenGL resources that need a valid context to be freed, and such a context probably doesn't exist at this point of execution.

You should fix your design to avoid globals, at least those holding resources.
Laurent Gomila - SFML developer

 

anything