Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:04:45+00:00 2026-05-24T20:04:45+00:00

I try to do simple drag’n’drop (drag file to text area). I implemented drag’n’drop

  • 0

I try to do simple drag’n’drop (drag file to text area). I implemented drag’n’drop in 2 ways. Lets mark them V1 and V2.

In both versions drag’n’drop works OK, but in V1 I get segmentation fault when I try to exit the application.

Question:

Maybe somebody could enlighten me why with V1 I get segmentation faul, while no segmentation fault with V2 ?
(I have no real problem using V2, just want to know the reason why segmenation fault occurs)

Short descriptions of the versions:

  • V1 – There is one class named Notepad. It inherits from wxFrame and wxFileDropTarget, and encapsulates wxTextCtrl and implements OnDropFiles(
  • V2 – class Notepad ingerits only from wxFrame and encapsulates wxTextCtrl. Drang’n’drop is done by separate class called DRPTARGET, which inherits from wxFileDropTarget and implements OnDropFiles(

Code for ilustration

(I cut out a lot of code here, which was not relevant. I hope I did not cut out too much)

V1:

    #include <wx/wx.h>
    #include <wx/dir.h>
    #include <wx/dnd.h>

    class Notepad : public wxFrame , public wxFileDropTarget {
        public:
            Notepad();
        private:
            wxTextCtrl* text_area;
            bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &filenames);
    };

    bool Notepad::OnDropFiles (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxArrayString &filenames){
        return this->text_area->LoadFile(filenames[0]);
    }

    Notepad::Notepad() : wxFrame(NULL, wxID_ANY, wxT("V1"), wxDefaultPosition, wxSize(650,500)) {
        wxBoxSizer *sizerh = new wxBoxSizer(wxHORIZONTAL);
        this->text_area = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER | wxTE_MULTILINE);
        sizerh->Add(this->text_area,1,wxEXPAND,0);
        this->SetSizer(sizerh);

        this->text_area->SetDropTarget(this);
    }

V2:

    #include <wx/wx.h>
    #include <wx/dir.h>
    #include <wx/dnd.h>


    class DRPTARGET : public wxFileDropTarget{
        private:
        wxTextCtrl* text_area;

        bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &filenames)
        {
            return this->text_area->LoadFile(filenames[0]);
        };

        public:
        DRPTARGET(wxTextCtrl* text_area)
        {
            this->text_area = text_area;
        };
    };



    class Notepad : public wxFrame , public wxFileDropTarget {
        public:
        Notepad(); // our default constructor


        private:
        wxTextCtrl* text_area;
    };


    Notepad::Notepad() : wxFrame(NULL, wxID_ANY, wxT("V2"), wxDefaultPosition, wxSize(650,500)) {
        wxBoxSizer *sizerh = new wxBoxSizer(wxHORIZONTAL);
        this->text_area = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER | wxTE_MULTILINE);
        sizerh->Add(this->text_area,1,wxEXPAND,0);
        this->SetSizer(sizerh);

        DRPTARGET* drop_target = new DRPTARGET(this->text_area);
        this->text_area->SetDropTarget(drop_target);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:04:45+00:00Added an answer on May 24, 2026 at 8:04 pm

    When a drop target is registered, wxWidgets will be nice and delete the pointer after the it exits the main loop. The problem has to do with the scope of the file drop target object.

    V1

    this->text_area->SetDropTarget(this);
    

    In this version, “this” refers to the frame. Remember that the window pointers are also managed by wxWidgets; the frame pointer is deleted before the file drop target. When wxWidgets tries to delete the drop target pointer it will attempt to delete a pointer that has already been deleted.

    V2

    DRPTARGET* drop_target = new DRPTARGET(this->text_area);
    this->text_area->SetDropTarget(drop_target);
    

    In this case, the pointer is allocated in the heap; but never deleted. In this case, when wxWidgets attempts to delete it it can do so safely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I try to copy an entire working copy using simple drag and drop
We have implemented a drag and drop feature that shows a nice simple representation
I tried to implement a simple drag and drop in the Android browser with
I'm trying to implement a simple drag/drop of images between panels using jQuery. While
I am making very simple test app, just to see how drag & drop
I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application and I'm using the MSDN code to
I am using a simple try-with-resources statement like the following: try (final CSVWriter w
I thought I'd try a simple GUI app using the world/universe mutation-free approach, but
in simple I try do that in PhpMyAdmin: update wp_postmeta.meta_value = wp_posts.id where wp_posts.post_name=articles.image
Hi I want to try making a simple application for android phones for which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.