MemoryBlock setSize() problem

Hi all!

I found that the following code is working incorrectly in the next case:

  • “Release” compiler configuration is used and the compiled program is running on the Intel machine.
#include "MainComponent.h"

const char text_file[] = "JUCE";

  PushMeButton.setButtonText("push me");
  setSize(400, 200);

MainComponent::~MainComponent() {

void MainComponent::paint(Graphics& g) {

void MainComponent::resized()
  PushMeButton.setBounds(0, 0, 200, 20);

void MainComponent::buttonClicked(Button* button)
  if (button == &PushMeButton) {

void MainComponent::pushMe() {
  MemoryBlock mb(text_file, sizeof(text_file));
  char *data = (char*) mb.getData();
  int size = mb.getSize() + 1;
  AlertWindow::showMessageBox(AlertWindow::NoIcon, "Data before", String(data, 4));
  mb.setSize(size, true);
  AlertWindow::showMessageBox(AlertWindow::NoIcon, "Data after", String(data, 4));

Just push the button few times and you’ll find out that “Data before” and “Data after” are different.

The code is working ok in the following cases:

  • “Debug” configuration is used.
  • the compiled program is running on the AMD machine.

I tested the code in VS2008 and VS2010, on stable/git versions of JUCE library.

Can someone please explain to me this strange behaviour? Thanks!

char *data = (char*) mb.getData();

That pointer becomes invalid as soon as the block is resized.

Oh, I’m feeling stupid now… :slight_smile: Thank you very much, Jules!